php artisan 迁移。定义了多个主键
php artisan migrate. Multiple primary key defined
我的问题
我正在使用 laravel 构建我的第一个 api 并在尝试 运行 php artisan migrate
时收到多个主键定义错误我不明白如何我定义了多个主键。每次我 运行 迁移时,我都会收到这些错误 php artisan migrate error.
我的疑难解答
我认为因为 autoIncrementing() 只能用于可能将其定义为主键的主键,所以我将 $table->increments('bus_id')->autoIncrement()->primary();
更改为 $table->increments('bus_id')->autoIncrement();
和 $table->increments('bus_id')->autoIncrement();
每次我尝试 运行 我的迁移时,我都会删除我的数据库并 re-created 它并再次尝试 运行 我的迁移(所以每次都是一个新数据库没有损坏的数据)但仍然没有什么不同。
我检查了上面错误代码图片中提到的 Connection.php,但没有看到与任何主键相关的任何信息。
我的问题
我的代码如下,有人可以帮我理解我是如何制作双主键的吗?
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePurchaseHistoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('purchase_history', function (Blueprint $table) {
$table->increments('pur_id', 11);
$table->string('pur_item', 255);
$table->dateTimeTz('pur_date');
$table->string('bus_name', 50);
$table->double('pur_amount', 10, 2);
$table->double('cashback_amount', 10, 2);
$table->integer('bus_id', 11);
$table->foreign('bus_id')->references('bus_id')->on('business');
$table->timestamps();
$table->rememberToken();
$table->engine = 'InnoDB';
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('purchase_history');
}
}
请注意 Stack overflow 上也有类似的问题,我也尝试过他们的解决方案但没有成功
编辑后我仍然收到此错误:
In Connection.php line 664:
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto colu
mn and it must be defined as a key (SQL: create table `purchase_history` (`pur_id` int unsigned not null auto_incre
ment primary key, `pur_item` varchar(255) not null, `pur_date` datetime not null, `bus_name` varchar(50) not null,
`pur_amount` double(10, 2) not null, `cashback_amount` double(10, 2) not null, `bus_id` int not null auto_increment
primary key, `created_at` timestamp null, `updated_at` timestamp null, `remember_token` varchar(100) null) default
character set utf8mb4 collate utf8mb4_unicode_ci engine = InnoDB)
宁运行后phpartisanmigrate:refresh
migrate:refresh error
business table
从您的 pk 定义中删除 autoincrements
和 primary
。
$table->increments('bus_id');
来自文档的定义:
Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column.
https://laravel.com/docs/5.5/migrations#columns
编辑:
increments
方法只接受一个参数:
$table->increments('pur_id');
https://laravel.com/api/5.3/Illuminate/Database/Schema/Blueprint.html#method_increments
编辑
问题是 bus_id
,它也以某种方式被设置为主键。来自异常:
`bus_id` int not null auto_increment primary key,
试试这些
- 删除
increments
上的值
- 如果
bus_id
是外键,则 unsigned
在添加 references
之前
- 重要提示:确保
category
在purchase_history
table和类别table之前有defined/createdauto-increment
字段是 bus_id
$table->increments('pur_id');
$table->integer('bus_id')->unsigned();
$table->foreign('bus_id')->references('bus_id')->on('category');
^ make sure category table auto increment field value is bus_id
仅供参考:如果我绘制 table,我将 id
设置为自动递增字段中的名称。因为没有混淆。
我的问题
我正在使用 laravel 构建我的第一个 api 并在尝试 运行 php artisan migrate
时收到多个主键定义错误我不明白如何我定义了多个主键。每次我 运行 迁移时,我都会收到这些错误 php artisan migrate error.
我的疑难解答
我认为因为 autoIncrementing() 只能用于可能将其定义为主键的主键,所以我将 $table->increments('bus_id')->autoIncrement()->primary();
更改为 $table->increments('bus_id')->autoIncrement();
和 $table->increments('bus_id')->autoIncrement();
每次我尝试 运行 我的迁移时,我都会删除我的数据库并 re-created 它并再次尝试 运行 我的迁移(所以每次都是一个新数据库没有损坏的数据)但仍然没有什么不同。
我检查了上面错误代码图片中提到的 Connection.php,但没有看到与任何主键相关的任何信息。
我的问题
我的代码如下,有人可以帮我理解我是如何制作双主键的吗?
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePurchaseHistoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('purchase_history', function (Blueprint $table) {
$table->increments('pur_id', 11);
$table->string('pur_item', 255);
$table->dateTimeTz('pur_date');
$table->string('bus_name', 50);
$table->double('pur_amount', 10, 2);
$table->double('cashback_amount', 10, 2);
$table->integer('bus_id', 11);
$table->foreign('bus_id')->references('bus_id')->on('business');
$table->timestamps();
$table->rememberToken();
$table->engine = 'InnoDB';
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('purchase_history');
}
}
请注意 Stack overflow 上也有类似的问题,我也尝试过他们的解决方案但没有成功 编辑后我仍然收到此错误:
In Connection.php line 664:
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto colu
mn and it must be defined as a key (SQL: create table `purchase_history` (`pur_id` int unsigned not null auto_incre
ment primary key, `pur_item` varchar(255) not null, `pur_date` datetime not null, `bus_name` varchar(50) not null,
`pur_amount` double(10, 2) not null, `cashback_amount` double(10, 2) not null, `bus_id` int not null auto_increment
primary key, `created_at` timestamp null, `updated_at` timestamp null, `remember_token` varchar(100) null) default
character set utf8mb4 collate utf8mb4_unicode_ci engine = InnoDB)
宁运行后phpartisanmigrate:refresh migrate:refresh error
business table
从您的 pk 定义中删除 autoincrements
和 primary
。
$table->increments('bus_id');
来自文档的定义:
Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column.
https://laravel.com/docs/5.5/migrations#columns
编辑:
increments
方法只接受一个参数:
$table->increments('pur_id');
https://laravel.com/api/5.3/Illuminate/Database/Schema/Blueprint.html#method_increments
编辑
问题是 bus_id
,它也以某种方式被设置为主键。来自异常:
`bus_id` int not null auto_increment primary key,
试试这些
- 删除
increments
上的值
- 如果
bus_id
是外键,则unsigned
在添加references
之前
- 重要提示:确保
category
在purchase_history
table和类别table之前有defined/createdauto-increment
字段是bus_id
$table->increments('pur_id');
$table->integer('bus_id')->unsigned();
$table->foreign('bus_id')->references('bus_id')->on('category');
^ make sure category table auto increment field value is bus_id
仅供参考:如果我绘制 table,我将 id
设置为自动递增字段中的名称。因为没有混淆。