【发布时间】:2020-10-25 02:23:20
【问题描述】:
我正在进行迁移,其中存在使用外键将一个表与其他各种表映射的特定迁移。并且在运行迁移时出现以下错误。
SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:更改表
bookmaps添加约束bookmaps_subject_id_foreign外键(subject_id)在删除级联时引用subject(id))
这是导致错误的迁移:
public function up()
{
Schema::create('bookmaps', function (Blueprint $table) {
$table->unsignedBigInteger('book_id')->unique();
$table->unsignedBigInteger('subject_id')->nullable();
$table->unsignedBigInteger('grade_id')->nullable();
$table->unsignedBigInteger('author_id')->nullable();
$table->unsignedBigInteger('catagory_id')->nullable();
$table->unsignedBigInteger('language_id')->nullable();
$table->timestamps();
});
Schema::table('bookmaps', function($table) {
$table->foreign('book_id')->references('id')->on('book')->onDelete('cascade');
$table->foreign('subject_id')->references('id')->on('subject')->onDelete('cascade');
$table->foreign('grade_id')->references('id')->on('grade')->onDelete('cascade');
$table->foreign('author_id')->references('id')->on('author')->onDelete('cascade');
$table->foreign('catagory_id')->references('id')->on('catagories')->onDelete('cascade');
$table->foreign('language_id')->references('id')->on('language')->onDelete('cascade');
});
}
其他相关表的迁移是:
public function up()
{
Schema::create('book', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('discription')->nullable();
$table->string('book_file')->nullable();
$table->string('card_image')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->timestamps();
});
Schema::table('book', function($table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
上述迁移创建的表仅通过外键映射,其余迁移未映射。
public function up()
{
Schema::create('subject', function (Blueprint $table) {
$table->id();
$table->string('subject_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('catagories', function (Blueprint $table) {
$table->id();
$table->string('catagories_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('language', function (Blueprint $table) {
$table->id();
$table->string('language_name');
$table->timestamps();
});
}
可能是什么问题?
【问题讨论】:
-
添加主键
-
有没有办法让 book_id 既作为外键又作为主键?因为它是一个用于映射的表,并且像一对一的关系一样使用。
-
添加自定义主键时,您需要在您的模型上定义它
protected $primaryKey = "book_id"; -
@STA 我没有立即创建模型文件,正在考虑稍后创建它。我认为在执行迁移时它应该不会影响。
-
是的,但是您的迁移,您需要在迁移中使用主键来建立关系
标签: php mysql laravel foreign-keys laravel-migrations