【发布时间】:2018-07-11 00:24:08
【问题描述】:
我的问题
我正在使用 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();
每次我尝试运行迁移时,我都会删除我的数据库并重新创建它并尝试再次运行我的迁移(所以它每次都是一个新数据库,没有损坏的数据)但仍然没有任何区别.
我检查了上面错误代码图片中提到的 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');
}
}
请注意这里有关于堆栈溢出的类似问题,我也尝试过他们的解决方案但没有任何成功 编辑后我仍然收到此错误:
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)
运行 php artisan migrate:refresh 后 migrate:refresh error
【问题讨论】:
-
$table->increments(...)创建一个自动递增的主键字段。所以->autoIncrement()->primary()都是多余的,可能会导致错误。 -
只要使用
$table->increments('bus_id'); -
好的,谢谢 我试试看。如果我记得,我不记得了。
-
@James 似乎不起作用
标签: php database laravel-5 migration laravel-5.5