mysql - Mysql2::Error:只能有一个自动列,并且必须将其定义为键
问题描述
尝试启动并运行 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
推荐阅读
- ruby-on-rails - 传递给 #or 的关系必须在结构上兼容 Rails 5.1 到 5.2
- c# - 无法从 c# Microsoft.Reporting.WebForms.ReportViewer 将参数传递给 SSRS 报告
- android - Android Room - 获取父母的所有基本孩子
- python - 创建 BMI 表
- c - 如果在 docker 容器中运行的 C 程序请求的内存超出容器允许的内存,会发生什么情况?
- r - system2 命令没有结果
- r - 闪亮的传单easyButton只触发一次
- kubernetes - 有人能解释一下不同的 Kubernetes yaml 文件和类型吗?
- docker - 将 docker-compose.yml 中的 ARG 传递给 docker 镜像
- python - 如何修改 staticmethod 装饰器行为?