0

this is my error:

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`))

I have table books and author with pivot table:

        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');

author table:

    public function up()
{
    Schema::create('authors', function (Blueprint $table) {
        $table->increments('id');
        $table->string('author');
        $table->date('birthdate');
        $table->timestamps();
    });
}

and my pivot table:

            $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();

help me please to find error!

Tanhaeirad
  • 169
  • 3
  • 13
  • 1
    Possible duplicate of [MySQL Error 1215: Cannot add foreign key constraint](https://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint) – ka_lin Feb 19 '19 at 13:16

1 Answers1

1

You misspelled the authors table. Forgot the s.

$table->foreign('author_id')->references('id')->on('authors');
//                                                        ^
Jerodev
  • 32,252
  • 11
  • 87
  • 108