这是我的错误:
General error: 1215 Cannot add foreign key constraint (SQL: alter table `author_book` add constraint `author_book_author_id_foreign` foreign key (`author_id`) references `author` (`id`))
我有桌书,并且作者带有数据透视表:
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('name');
$table->unsignedInteger('pages');
$table->string('ISBN');
$table->integer('price');
$table->date('published_at');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
作者表:
public function up()
{
Schema::create('authors', function (Blueprint $table) {
$table->increments('id');
$table->string('author');
$table->date('birthdate');
$table->timestamps();
});
}
和我的数据透视表:
$table->increments('id');
$table->unsignedInteger('author_id');
$table->unsignedInteger('book_id');
$table->foreign('author_id')->references('id')->on('author');
$table->foreign('book_id')->references('id')->on('books');
$table->timestamps();
请帮助我发现错误!
答案 0 :(得分:1)
您拼写了作者 s 表。忘记了s
。
$table->foreign('author_id')->references('id')->on('authors');
// ^