我正在尝试更改表上的主键,以便我也可以使用其他列作为外键。
这是创建的迁移
public function up()
{
Schema::create('blacklists', function (Blueprint $table) {
$table->id();
$table->integer('adv_id')->default(0);
// ...other columns
});
}
然后,我进行了另一次迁移以更改 adv_id 列类型,以便准备添加外键
public function up()
{
Schema::table('blacklists', function(Blueprint $table){
$table->integer('adv_id')->unsigned()->change();
});
}
这里是我的库存
public function up()
{
Schema::table('blacklists', function(Blueprint $table) {
$table->dropPrimary('id');
$table->integer('adv_id')->primary()->change();
});
}
当我运行上次迁移时,我收到此错误消息
Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: alter table `blacklists` drop primary key)
最佳答案
这对我有用
public function up()
{
Schema::table('blacklists', function (Blueprint $table) {
$table->dropPrimary();
$table->unsignedInteger('id')->change();
$table->dropColumn('id');
$table->uuid()->primary()->unique()->index()->first();
});
关于php - 如何更改 Laravel 迁移中的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72170674/