Mysql 外键上的Laravel迁移错误
我正在尝试创建此迁移:Mysql 外键上的Laravel迁移错误,mysql,laravel,Mysql,Laravel,我正在尝试创建此迁移: public function up() { // Create the `Dashboards` table Schema::create('dashboards', function(Blueprint $table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->string('name');
public function up()
{
// Create the `Dashboards` table
Schema::create('dashboards', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->tinyInteger('is_public');
$table->timestamps();
});
// Creates the user_dashboard (Many-to-Many relation) table
Schema::create('user_dashboard', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('dashboard_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('dashboard_id')->references('id')->on('dashboards')->onDelete('cascade');
});
}
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->boolean('active')->default(0);
$table->string('confirmation_code')->nullable();
$table->rememberToken();
$table->string('avatar')->nullable();
$table->string('provider')->nullable();
$table->string('provider_token')->unique()->nullable();
$table->timestamps();
});
运行迁移时,出现以下错误:
SQLSTATE[HY000]: General error: 1005 Can't create table
'prestiti_protestatinet.#sql-358f_9e6a9' (errno: 150) (SQL: alter table
`dl_user_dashboard` add constraint user_dashboard_user_id_foreign foreign
key (`user_id`) references `dl_users` (`id`) on delete cascade)
这适用于我的本地环境(wampserver),但不适用于生产环境(lamp)。这里怎么了
我的用户迁移:
public function up()
{
// Create the `Dashboards` table
Schema::create('dashboards', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->tinyInteger('is_public');
$table->timestamps();
});
// Creates the user_dashboard (Many-to-Many relation) table
Schema::create('user_dashboard', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('dashboard_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('dashboard_id')->references('id')->on('dashboards')->onDelete('cascade');
});
}
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->boolean('active')->default(0);
$table->string('confirmation_code')->nullable();
$table->rememberToken();
$table->string('avatar')->nullable();
$table->string('provider')->nullable();
$table->string('provider_token')->unique()->nullable();
$table->timestamps();
});
您发布的错误消息表明外键约束的格式不正确。表引擎类型似乎有问题。我没有指定
$table->engine='InnoDB'
在我的users表上,即MyIsam,可能是由于某些数据库默认设置……我希望您没有在此处创建users表,并且对ID使用unsigned,请检查它是否可以解决您的问题您的user
表迁移架构在哪里?我已经用我的用户迁移更新了我的问题。我认为这个问题与数据库引擎模式有关:默认情况下,数据库创建myisam
表,但在我的用户面板表中,我指定了$table->engine='InnoDB'代码>。那么我应该在每次迁移时指定Innodb吗?您发布的错误消息表明外键约束的格式不正确。表引擎类型似乎有问题。我没有指定$table->engine='InnoDB'
在我的users表上,即MyIsam,可能是由于某些数据库默认设置……我希望您没有在此处创建users表,并且对ID使用unsigned,请检查它是否可以解决您的问题您的user
表迁移架构在哪里?我已经用我的用户迁移更新了我的问题。我认为这个问题与数据库引擎模式有关:默认情况下,数据库创建myisam
表,但在我的用户面板表中,我指定了$table->engine='InnoDB'代码>。那么我应该在每次迁移时指定Innodb吗?