【发布时间】:2018-06-01 22:48:20
【问题描述】:
我对 Laravel、PHP 和 MySql 的了解有限。我正在尝试使用外键建立一个包含 3 个连接表的数据库。我之前在 WorkBench MySql 中手动完成了此操作,但在 Laravel 中无法完成。这是我的错误和代码。
SQL STATE[42000]: Syntax error or access violation: 1072 Key column 'cat_id
' doesn't exist in table (SQL: alter table `users` add index `users_cat_id
_usr_id_index`(`cat_id`, `usr_id`))
表 1
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('usr_id')->autoIncrement();
$table->string('usr_email',45)->unique();
$table->string('usr_password',256);
$table->rememberToken();
$table->string('usr_name',45);
$table->string('usr_surname',45);
$table->string('usr_title',45);
$table->string('usr_psc',5);
$table->string('usr_region',128);
$table->string('usr_adress',128);
$table->string('usr_tel',10);
$table->string('usr_tel_int',13);
$table->binary('usr_image');
$table->tinyInteger('usr_type');
$table->index(['cat_id','usr_id'])->nullable();
$table->index(['tem_id','usr_id'])->nullable();
});
}
表2
public function up()
{
Schema::create('category', function (Blueprint $table) {
$table->primary('cat_id');
$table->string('cat_name',45);
});
Schema::table('category', function(Blueprint $table) {
$table->foreign('cat_id')->references('cat_id')->on('users');
});
}
表3
public function up()
{
Schema::create('team', function (Blueprint $table) {
$table->increments('tem_id');
$table->unique('tem_sub_id');
$table->string('tem_name',45);
$table->string('tem_subteam_name',45);
});
Schema::table('team', function(Blueprint $table) {
$table->foreign('tem_id')->references('tem_id')->on('users');
});
}
【问题讨论】:
-
如果您在正确创建迁移时遇到问题,我建议您使用laravelsd.com,它是生成迁移文件的在线工具
标签: php mysql laravel database-migration