代码之家  ›  专栏  ›  技术社区  ›  John Bachir

什么决定rails在表定义中是否包含id::serial?

  •  1
  • John Bachir  · 技术社区  · 7 年前

    我正在使用现有的rails应用程序,使用postgresql。它的schema.rb文件 id: :serial

    create_table "foos", id: :serial, force: :cascade do |t|
    

    当我跑的时候 rails db:migrate:reset , 序列号

    rails版本与项目启动时的版本相同。

    该项目确实是从sqlite3开始的。当我切换到该选项并重新生成文件时,会出现相同的行为。

    是什么原因导致此选项在我的环境中被删除?

    下面是一些可能相关的代码:

    • 我刚试过 rails数据库:迁移:重置 在同事的机器上,我错了!它们的环境也会移除 .
    • 在schema.rb中。
    2 回复  |  直到 7 年前
        1
  •  17
  •   Old Pro    7 年前

    当你跑的时候 rails db:migrate:reset 相对于 rails db:reset schema.rb id 字段,默认情况下提供一个。但是,从Rails 5.1开始 default size of the id field was increased INT BIGINT SERIAL BIGSERIAL 对于PostgreSQL。因此,迁移之间可能存在一些交互, schema.rb ,以及数据库中的实际模式,该模式在某些情况下导致id字段被视为默认值(并被忽略),而在其他情况下则由于默认大小的更改而被显式指定。在没有看到所有相关文件的情况下,很难猜测问题的根源。

        2
  •  11
  •   John Bachir    7 年前

    答案就是rails 5.0对5.1的迁移。我以前认为这个项目是从5.1开始的,所以我没有测试这个。但后来我深入研究,发现它是从5.0开始的,实验表明这就是答案。

    5.0,未指定id

    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)
    

    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,指定的序列号

    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
    
    t、 字符串“foo_角色”
    终止
    
    #\d测试
    表“public.test”
    ------------------+-------------------+-------------------------------------------------------
    foo_id|整数|
    索引:
    “测试密钥”主键,btree(id)