php - 在具有现有主键的表中添加自动递增 id
问题描述
我正在尝试使用其现有主键将自动递增 id 添加到现有表中
这是我的代码
表代码的初始迁移
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->string('ISBN')->index();
$table->string('name')->index();
$table->string('publisher');
$table->string('level_levelName')->index();
$table->string('provider_providerName')->index();
$table->string('category_categoryName')->index();
$table->text('description');
$table->timestamps();
$table->primary('ISBN');
$table->foreign('provider_providerName')->references('providerName')->on('providers')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('level_levelName')->references('levelName')->on('levels')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('category_categoryName')->references('categoryName')->on('categories')->onDelete('cascade')->onUpdate('cascade');
});
}
将自动增量 id 添加到现有表代码
public function up()
{
Schema::table('books', function (Blueprint $table) {
$table->bigIncrements('id');
});
}
我想要做的是向这个现有表添加一个自动递增的 id,但它给了我这个错误
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary keys defined (SQL: alter table
`books` add `id` bigint unsigned not null auto_increment primary key)
请问有人可以帮我吗?我不想删除表的主键,我只想添加另一个自动递增的 id,它不是主键,但它可以是unique key
解决方案
推荐阅读
- docker - 如何在 docker-compose 中设置环境变量
- excel - 如何在VBA中将文件名调用到excel公式中
- c# - 从旧 Azure 库(Microsoft.ServiceBus.Messaging 命名空间)中的抽象类继承。找不到合适的方法来覆盖
- android - 如何使用 ionic 3 @ionic-native/http 在本机 HTTP 中传递 FormData?
- r - 无法访问 dbplyr 中的字符串方法
- c# - 有没有办法合并两个具有相同键的对象数组,总结另一个属性的值?
- error-handling - Spring Integration - 如何将错误处理委托给单独的线程
- windows - 如何识别对 DLL 进行了哪些调用?
- reactjs - React material-ui grid:是否可以添加断点?
- angular - 如何调用订阅?