gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:30:54 25 4
gpt4 key购买 nike

尝试启动并运行 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

错误消失。为什么会发生这种情况以及为什么该架构在我的设置中不起作用?

最佳答案

错误消息告诉您只有列可以自动递增,并且该列也必须是主键。因此,尝试将 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

关于Mysql2::Error:只能有一个自动列,并且必须将其定义为键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53919087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com