eloquent - Laravel 5.8:总是给出多个主键语法或访问冲突错误
问题描述
我用复合主键创建了迁移文件,但总是报错
语法错误或访问冲突:1068 定义了多个主键(sql:alter table 'table_currency' 添加主键 table_currency_code_user_id_primary('code', 'user_id'))
Schema::create('table_currency', function (Blueprint $table) {
$table->string('code', 3);
$table->bigIncrements('user_id');
$table->string('default', 3);
$table->enum('is_active', ['0','1'])->default('0')->comment('0: Inactive, 1: Active');
$table->timestamps();
$table->primary(['code', 'user_id']);
});
我不明白为什么我得到这个错误?提前谢谢。
解决方案
如果要创建复合主键,则['user_id', 'code']
需要删除increments
列上附加的主键。您可以使用以下代码执行此操作:
Schema::create('table_currency', function (Blueprint $table) {
$table->bigIncrements('user_id'); // Has a primary key
$table->dropPrimary('table_currency_user_id_primary'); // Remove the primary key from the increments column
$table->primary(['code', 'user_id']); // Set your composite primary key
$table->string('code', 3);
$table->string('default', 3);
$table->enum('is_active', ['0','1'])->default('0')->comment('0: Inactive, 1: Active');
$table->timestamps();
});
推荐阅读
- javascript - iOS 上的 PWA 在单击共享按钮时导航到空白屏幕
- amazon-web-services - SQS使用php的最大消息数
- loadrunner - 数据库连接失败 {“'OraOLEDB.Oracle.1' 提供程序未在本地计算机上注册。”}
- excel - 变量可以与命名范围同名吗?
- javascript - 如何使用 Angular 从 Web Api 获取数据?
- red-black-tree - red_black_tree 消除混乱
- c# - 无法通过 c# 中的 form.close() 关闭表单
- angular - 角度未检测到创建的服务
- python-3.x - 转换时间戳
- c# - 带有 InvokeCommandAction 的 ListView 阻碍 SelectedItem 绑定