我使用此设置进行了迁移:
$table->increments('id');
$table->integer('user_id', 10)->unsigned(); // this is meant to be used as a foreign key
Run Code Online (Sandbox Code Playgroud)
做了php artisan migrate后,它返回一个错误:
[Exception]
SQLSTATE[42000]: 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: create table `transactions` (`id` int unsigned not null auto_increment primary key, `user_id` int unsigned not null auto_increment primary key) default character set utf8 collate utf8_unicode_ci) (Bindings: array ())
Run Code Online (Sandbox Code Playgroud)
我没有将user_id指定为auto_increment主键,但是Migration会将其视为auto_increment主键.
如何在迁移中创建外键?
当我做php artisan migrate时,我收到此错误.我的迁移文件有什么问题吗?或者我的模型可能编码错误吗?但即使模型中存在问题,迁移也应该有效吗?
[Exception]
SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-
16643_2033' (errno: 150) (SQL: alter table `gigs` add constraint gigs_band_
id_foreign foreign key (`band_id`) references `bands` (`band_id`) on delete
cascade) (Bindings: array (
))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-
16643_2033' (errno: 150)
Run Code Online (Sandbox Code Playgroud)
演出迁移
public function up()
{
Schema::create('gigs', function($table)
{
$table->increments('gig_id');
$table->dateTime('gig_startdate');
$table->integer('band_id')->unsigned();
$table->integer('stage_id')->unsigned();
$table->foreign('band_id')
->references('band_id')->on('bands')
->onDelete('cascade');
$table->foreign('stage_id')
->references('stage_id')->on('stages')
->onDelete('cascade');
});
public function down()
{
Schema::table('gigs', function($table)
{
Schema::drop('gigs');
$table->dropForeign('gigs_band_id_foreign');
$table->dropForeign('gigs_stage_id_foreign');
});
}
Run Code Online (Sandbox Code Playgroud)
乐队迁移 …