Rails5 - 如何db:设置新机器并包含数据库自动增量id序列

时间:2016-06-29 18:38:02

标签: ruby-on-rails postgresql activerecord migration ruby-on-rails-5

我有一个从头开始构建的Rails5 api以及我自己创建的所有模型的id自动增量数据库序列。当一位同事尝试db:在新机器上设置数据库时,架构已加载,但序列未创建,因此我们的db:种子无法添加,因为有一个“not null”约束表。

原始表ID列如下所示

id  | integer | not null default nextval('users_id_seq'::regclass) | plain    |

运行db:setup后,新机器上的“new”数据库如下所示

id  | integer | not null  | plain    |

我以前的Rails版本从来没有遇到这个问题,并且想知道它是否可能是v5问题。我们做错了什么?

感谢您的任何提示!

在新数据库中,运行“\ ds;”

时,postgres中没有显示序列

users表的迁移看起来像这样

class CreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      t.string :first_name, limit: 25
      t.string :last_name, limit: 25
      t.string :mobile_phone, limit: 25
      t.string :auth_token, limit: 36
      t.integer :failed_login_attempts, limit: 2, :default => 0
      t.boolean :account_locked, :default => false
      t.timestamps
    end
  end
end

此表的schema.rb如下所示(在稍后的某些迁移之后)

create_table "users", id: :integer, force: :cascade do |t|
    t.string   "first_name",             limit: 25
    t.string   "last_name",              limit: 25
    t.string   "mobile_phone",           limit: 25
    t.string   "auth_token",             limit: 36
    t.integer  "failed_login_attempts",  limit: 2,  default: 0
    t.boolean  "account_locked",                    default: false
    t.datetime "created_at",                                           null: false
    t.datetime "updated_at",                                           null: false
    t.string   "email",                             default: "",       null:         
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
  end

0 个答案:

没有答案