Rails pg问题与生产的偏移查询

时间:2015-01-03 23:00:54

标签: sql ruby-on-rails ruby

以下陈述产生问题:

Lang.limit(10).offset(0)

在我的开发环境中,rails console产生以下结果:

2.1.1 :001 > Lang.limit(10).offset(0)
  Lang Load (1.2ms)  SELECT "langs".* FROM "langs" LIMIT 10 OFFSET 0
 => [#<Lang id: 1, english: "be", english_to_spanish: "ser", spanish: "una", spanish_to_english: "a", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">, #<Lang id: 2, english: "I", english_to_spanish: "Yo", spanish: "no", spanish_to_english: "do not", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">, #<Lang id: 3, english: "you", english_to_spanish: "usted", spanish: "pero", spanish_to_english: "but", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">, #<Lang id: 4, english: "with", english_to_spanish: "con", spanish: "la", spanish_to_english: "the", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">, #<Lang id: 5, english: "the", english_to_spanish: "la", spanish: "que", spanish_to_english: "that", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">, #<Lang id: 6, english: "they", english_to_spanish: "ellos", spanish: "y", spanish_to_english: "and", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">, #<Lang id: 7, english: "that", english_to_spanish: "que", spanish: "por", spanish_to_english: "by", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">, #<Lang id: 8, english: "this", english_to_spanish: "este", spanish: "lo", spanish_to_english: "it", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">, #<Lang id: 9, english: "are", english_to_spanish: "son", spanish: "se", spanish_to_english: "is", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">, #<Lang id: 10, english: "a", english_to_spanish: "un", spanish: "con", spanish_to_english: "with", created_at: "2015-01-03 20:30:35", updated_at: "2015-01-03 20:30:35">] 
2.1.1 :002 > 

在我的生产环境中,rails console产生以下结果:

irb(main):002:0> Lang.limit(10).offset(0)
  Lang Load (2.4ms)  SELECT "langs".* FROM "langs" LIMIT 10 OFFSET 0
=> [#<Lang id: 4172, english: "judicial", english_to_spanish: "judicial", spanish: "habitaciones", spanish_to_english: "room", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">, #<Lang id: 4173, english: "diamond", english_to_spanish: "diamante", spanish: "afortunadamente", spanish_to_english: "fortunately", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">, #<Lang id: 4174, english: "alternatives", english_to_spanish: "alternativas", spanish: "detener", spanish_to_english: "stop", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">, #<Lang id: 4175, english: "sighed", english_to_spanish: "suspirado", spanish: "antecedentes", spanish_to_english: "background", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">, #<Lang id: 4176, english: "surveys", english_to_spanish: "encuestas", spanish: "historiador", spanish_to_english: "historian", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">, #<Lang id: 4177, english: "managing", english_to_spanish: "gerente", spanish: "sobrevivir", spanish_to_english: "survive", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">, #<Lang id: 4178, english: "bottles", english_to_spanish: "botellas", spanish: "salvación", spanish_to_english: "salvation", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">, #<Lang id: 4179, english: "productive", english_to_spanish: "productivo", spanish: "renuncia", spanish_to_english: "resignation", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">, #<Lang id: 4180, english: "quarters", english_to_spanish: "cuarteles", spanish: "saliendo", spanish_to_english: "leaving", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">, #<Lang id: 4181, english: "arrangement", english_to_spanish: "disposición", spanish: "persecución", spanish_to_english: "persecution", created_at: "2015-01-03 20:28:01", updated_at: "2015-01-03 20:28:01">]
irb(main):003:0>

从开发中可以看出,这产生了Lang ID的1..10,但在生产中它产生了朗ID的4172..4181。有关为何存在差异以及如何让生产环境像开发环境一样的想法?

1 个答案:

答案 0 :(得分:2)

如果未在SQL中指定特定顺序,则可以按照数据库所需的顺序获取行,这很少是您想要的顺序。所以,如果你想要特定顺序的东西,请这样说:

Lang.order(:id).offset(0).limit(10)
# ---^^^^^^^^^^

这将为SQL添加一个ORDER BY子句,并以预期的顺序为您提供服务。

您在开发环境中完全按照顺序处理事情,不能保证在执行一些INSERT,DELETE和UPDATE之后明天或下周都会按顺序获取它们。