我有2个应用通过heroku database sharing tool使用相同的数据库。我注意到,正在向客户端提供某种方式存在于两个数据库中的一个数据库中的数据。当我说2个数据库时,我的意思是分别从每个应用程序运行heroku run rails c
可以访问的数据。
应用1:
> pp c.weigh_ins.pluck(:id, :date, :current_weight)
D, [2018-10-16T14:47:43.016457 #4] DEBUG -- : (30.4ms) SELECT "weigh_ins"."id", "weigh_ins"."date", "weigh_ins"."current_weight" FROM "weigh_ins" WHERE "weigh_ins"."client_id" = $1 [["client_id", 19305]]
[[139358, Tue, 09 Oct 2018 00:00:00 EDT -04:00, 170.0],
[138723, Tue, 02 Oct 2018 00:00:00 EDT -04:00, 217.2], # where does this come from?
[138708, Tue, 02 Oct 2018 00:00:00 EDT -04:00, 174.6],
[137962, Tue, 25 Sep 2018 00:00:00 EDT -04:00, 176.6],
[137243, Tue, 18 Sep 2018 00:00:00 EDT -04:00, 175.8],
[136489, Tue, 11 Sep 2018 00:00:00 EDT -04:00, 177.0],
[135792, Tue, 04 Sep 2018 00:00:00 EDT -04:00, 178.8],
[135217, Tue, 28 Aug 2018 00:00:00 EDT -04:00, 182.4],
[134593, Tue, 21 Aug 2018 00:00:00 EDT -04:00, 184.2],
[133950, Tue, 14 Aug 2018 00:00:00 EDT -04:00, 186.4],
[133320, Tue, 07 Aug 2018 00:00:00 EDT -04:00, 188.8],
[132630, Tue, 31 Jul 2018 00:00:00 EDT -04:00, 190.6],
[132053, Fri, 20 Jul 2018 00:00:00 EDT -04:00, 194.4]]
应用2:
> pp c.weigh_ins.pluck(:id, :date, :current_weight)
(15.8ms) SELECT "weigh_ins"."id", "weigh_ins"."date", "weigh_ins"."current_weight" FROM "weigh_ins" INNER JOIN "check_ins" ON "weigh_ins"."check_in_id" = "check_ins"."id" WHERE "weigh_ins"."deleted_at" IS NULL AND "check_ins"."deleted_at" IS NULL AND "check_ins"."client_id" = $1 ORDER BY "check_ins"."week" DESC, "check_ins"."week" ASC [["client_id", 19305]]
(15.8ms) SELECT "weigh_ins"."id", "weigh_ins"."date", "weigh_ins"."current_weight" FROM "weigh_ins" INNER JOIN "check_ins" ON "weigh_ins"."check_in_id" = "check_ins"."id" WHERE "weigh_ins"."deleted_at" IS NULL AND "check_ins"."deleted_at" IS NULL AND "check_ins"."client_id" = $1 ORDER BY "check_ins"."week" DESC, "check_ins"."week" ASC [["client_id", 19305]]
[[139358, Tue, 09 Oct 2018 00:00:00 EDT -04:00, 170.0],
[138708, Tue, 02 Oct 2018 00:00:00 EDT -04:00, 174.6],
[137962, Tue, 25 Sep 2018 00:00:00 EDT -04:00, 176.6],
[137243, Tue, 18 Sep 2018 00:00:00 EDT -04:00, 175.8],
[136489, Tue, 11 Sep 2018 00:00:00 EDT -04:00, 177.0],
[135792, Tue, 04 Sep 2018 00:00:00 EDT -04:00, 178.8],
[135217, Tue, 28 Aug 2018 00:00:00 EDT -04:00, 182.4],
[134593, Tue, 21 Aug 2018 00:00:00 EDT -04:00, 184.2],
[133950, Tue, 14 Aug 2018 00:00:00 EDT -04:00, 186.4],
[133320, Tue, 07 Aug 2018 00:00:00 EDT -04:00, 188.8],
[132630, Tue, 31 Jul 2018 00:00:00 EDT -04:00, 190.6],
[132053, Fri, 20 Jul 2018 00:00:00 EDT -04:00, 194.4]]
由于两个应用程序都具有相同的DATABASE_URL
,所以它们都不应该访问相同的数据库吗?有什么办法可以使它们不同步吗?我意识到他们有不同的查询,但他们应该返回相同的数据。
答案 0 :(得分:1)
您在应用程序中的查询有所不同。主要是,您的第一个应用程序确实会返回已删除的记录,因为没有deleted_at IS NULL
的条件(第二个应用程序还会执行一个附加的INNER JOIN
,这可能会过滤出更多结果)。如果您使用的是ActiveRecord模型,则可能忘记了第一个应用程序中的默认作用域。