Mysql2 :: Error:只能有一个自动列,并且必须将其定义为键

时间:2018-12-25 03:20:52

标签: mysql ruby-on-rails schema

尝试启动并运行Rails应用程序。我从同事在schema.rb中给出的以下表定义中得到此错误。

ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect table definition; there can be only one auto column and it must be defined as a key 

表定义:

  create_table "wv latest", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.integer "id", null: false, auto_increment: true
    t.string "cid", limit: 10
    t.integer "visit_id"
    t.string "cfname", limit: 20
    t.string "clname", limit: 25
  end

当我删除

auto_increment: true

错误消失了。为什么会发生这种情况,为什么该模式在我的设置中不起作用?

1 个答案:

答案 0 :(得分:0)

错误消息告诉您,只有该列可以自动递增,并且该列也必须是主键。因此,请尝试将id列设为主键:

create_table "wv latest", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.integer "id", null: false, auto_increment: true, primary_key: true
    t.string "cid", limit: 10
    t.integer "visit_id"
    t.string "cfname", limit: 20
    t.string "clname", limit: 25
end