'How to change primary key in laravel migration
I'm trying to change the primary key on a table so I can use an other column as foreign key too.
Here's the migration for the creation
public function up()
{
Schema::create('blacklists', function (Blueprint $table) {
$table->id();
$table->integer('adv_id')->default(0);
// ...other columns
});
}
Then, I've made an other migration to change the adv_id column type in order to prepare the addition of a foreign key
public function up()
{
Schema::table('blacklists', function(Blueprint $table){
$table->integer('adv_id')->unsigned()->change();
});
}
Here's where I'm stocked
public function up()
{
Schema::table('blacklists', function(Blueprint $table) {
$table->dropPrimary('id');
$table->integer('adv_id')->primary()->change();
});
}
When I run the last migration, I got this error message
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)
Solution 1:[1]
You can try this because at a time only one auto-increment allow.
public function up()
{
Schema::table('blacklists', function (Blueprint $table) {
$table->dropPrimary('id');
$table->integer('adv_id')->unsigned()->change();
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shravan Goswami |
