是什么决定了rails是否在表定义中包含id::serial?
我正在使用postgresql使用现有的Rails应用程序。对于许多(但不是全部)表,其schema.rb文件具有 id::serial
:
I'm working with an existing rails app, using postgresql. Its schema.rb file has id: :serial
for many, but not all, tables:
create_table "foos", id: :serial, force: :cascade do |t|
当我运行 rails db:migrate:reset
, id::serial
被删除。我们都使用相同版本的postgres,但使用不同的操作系统。我尚未详尽测试机器之间的行为,但我认为机器之间存在差异。
When I run rails db:migrate:reset
, id: :serial
is removed. We are all on the same version of postgres, but different OSes. I haven't exhaustively tested the behavior between machines, but I think there is a difference between machines.
rails版本与项目开始时的版本相同。
The rails version is the same as it was when the project started.
该项目确实从sqlite3开始。当我切换到该文件并重新生成文件时,行为相同。
The project did start with sqlite3. When I switch to that and regenerate the file, same behavior.
什么会导致在我的环境中删除此选项?
What could cause this option to be removed in my environment?
下面是一些可能相关的代码:
here's some code that is probably relevant:
- https://github.com/rails/rails/blob/b2eb1d1c55e59 active_record / connection_adapters / postgresql / column.rb#L15-L21
- https://github.com/rails/rails/blob/b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c/activerecord/lib/active_record/poster_umper .rb#L26-L42
- https://github.com/rails/rails/blob/b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c/activerecord/lib/active_record/connection_adapters/postgresql/column.rb#L15-L21
- https://github.com/rails/rails/blob/b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb#L26-L42
更新ong>
- 我刚刚在同事的机器上尝试了
rails db:migrate:reset
,我错了!他们的环境还删除了id :: serial
。 - 我仔细查看了最近从一位同事处进行的迁移,但最近一次没有要么在schema.rb中创建
id :: serial
。
- I just tried
rails db:migrate:reset
on colleague's machines, and I was wrong! their environments also removeid: :serial
. - I looked closer at recent migrations from a colleague, and the most recent one did not create
id: :serial
in schema.rb either.
答案仅是Rails 5.0与5.1迁移。我以前曾以为该项目始于5.1,所以我没有对此进行测试。但是后来我更深入地研究,发现它始于5.0,实验表明这就是答案。
The answer is simply rails 5.0 vs 5.1 migrations. I had previously thought that the project started in 5.1, so I didin't test this. But then I dug deeper and discovered it started in 5.0, and experimentation shows that's the answer.
class SerialIdTest < ActiveRecord::Migration[5.0]
def change
create_table "test" do |t|
t.integer "foo_id"
t.string "foo_role"
end
end
end
create_table "test", id: :serial, force: :cascade do |t|
t.integer "foo_id"
t.string "foo_role"
end
# \d test
Table "public.test"
Column | Type | Modifiers
------------------+-------------------+-------------------------------------------------------
id | integer | not null default nextval('test_id_seq'::regclass)
foo_id | integer |
foo_role | character varying |
Indexes:
"test_pkey" PRIMARY KEY, btree (id)
5.1 ,未指定ID
5.1, no id specified
class SerialIdTest < ActiveRecord::Migration[5.1]
def change
create_table "test" do |t|
t.integer "foo_id"
t.string "foo_role"
end
end
end
create_table "test", force: :cascade do |t|
t.integer "foo_id"
t.string "foo_role"
end
# \d test
Table "public.test"
Column | Type | Modifiers
------------------+-------------------+-------------------------------------------------------
id | bigint | not null default nextval('test_id_seq'::regclass)
foo_id | integer |
foo_role | character varying |
Indexes:
"test_pkey" PRIMARY KEY, btree (id)
5.1 ,指定的ID序列
5.1, id serial specified
class SerialIdTest < ActiveRecord::Migration[5.1]
def change
create_table "test", id: :serial do |t|
t.integer "foo_id"
t.string "foo_role"
end
end
end
create_table "test", id: :serial, force: :cascade do |t|
t.integer "foo_id"
t.string "foo_role"
end
# \d test
Table "public.test"
Column | Type | Modifiers
------------------+-------------------+-------------------------------------------------------
id | integer | not null default nextval('test_id_seq'::regclass)
foo_id | integer |
foo_role | character varying |
Indexes:
"test_pkey" PRIMARY KEY, btree (id)