MYSQL/Laravel 迁移将复合键更改为自动增量
MYSQL/Laravel Migrations Change composite key to autoincrement
我有一个包含两行的枢轴 table (exercise_task)。目前这两行构建了一个复合主键。
不幸的是,事实证明这是一个糟糕的想法,因为现在我需要能够多次将相同的任务添加到练习中。
因此我要么需要切换到自动递增主键。
此外,我还需要添加一个索引行(表示顺序),这样也可以扩展复合键。
问题
实现这一目标的最佳方法是什么?由于数据库已经在使用中,我不能只删除 table。
原始迁移
Schema::create('exercise_task', function (Blueprint $table) {
$table->integer('exercise_id')->unsigned();
$table->integer('task_id')->unsigned();
$table->foreign('exercise_id')->references('id')->on('exercises')
->onUpdate('CASCADE')->onDelete('CASCADE');
$table->foreign('task_id')->references('id')->on('tasks')
->onUpdate('CASCADE')->onDelete('CASCADE');
$table->primary(['exercise_id', 'task_id']);
});
我建议重构您的代码以从头开始解决此问题,或者在 MySQL 终端 window 中使用以下代码添加索引行:
INSERT INTO name
VALUES ('column1 value', 'column2 value', 'column3 value', 'column4 value');
我得到以下结果:
1.删除pivot的所有外键table (This was the key)
2. 删除主键
3.添加新的主键(我决定通过新的索引列扩展组合键)
4.读取之前丢弃的外键。
我有一个包含两行的枢轴 table (exercise_task)。目前这两行构建了一个复合主键。
不幸的是,事实证明这是一个糟糕的想法,因为现在我需要能够多次将相同的任务添加到练习中。
因此我要么需要切换到自动递增主键。
此外,我还需要添加一个索引行(表示顺序),这样也可以扩展复合键。
问题
实现这一目标的最佳方法是什么?由于数据库已经在使用中,我不能只删除 table。
原始迁移
Schema::create('exercise_task', function (Blueprint $table) {
$table->integer('exercise_id')->unsigned();
$table->integer('task_id')->unsigned();
$table->foreign('exercise_id')->references('id')->on('exercises')
->onUpdate('CASCADE')->onDelete('CASCADE');
$table->foreign('task_id')->references('id')->on('tasks')
->onUpdate('CASCADE')->onDelete('CASCADE');
$table->primary(['exercise_id', 'task_id']);
});
我建议重构您的代码以从头开始解决此问题,或者在 MySQL 终端 window 中使用以下代码添加索引行:
INSERT INTO name
VALUES ('column1 value', 'column2 value', 'column3 value', 'column4 value');
我得到以下结果:
1.删除pivot的所有外键table (This was the key)
2. 删除主键
3.添加新的主键(我决定通过新的索引列扩展组合键)
4.读取之前丢弃的外键。