Issue with Laravel Migrations and Foreign Keys

jerney

I am currently receiving the following error when I run my migrations:

SQLSTATE[HY000]: General error: 1005 Can't create table 'saferides.#sql-189_4bc' (errno: 150) (SQL: alter table `rides` add constraint rides_car_id_foreign foreign key (`car_id`) references `car` (`id`) on delete cascade)

After reading a lot of issues about errors of this nature in Laravel, I have done the following to avoid it:

  • Not reference a primary key with a foreign key until the primary key is created ( I make sure of this by keeping my table creations and foreign key assignments in separate migrations in the following order creation -> keys
  • Make sure the primary key and the foreign key are of the same type (unsigned int(10))
  • Explicitly state the storage engine to be InnoDB

Currently, I have my migrations set up so that the tables are created first:

// Up function for cars table
public function up()
{
    Schema::create('cars', function(Blueprint $table)
    {
        // Explicitly state storage engine
        $table->engine = 'InnoDB';

        // Primary Key
        $table->increments('id')->unsigned();

        // Other columns
        $table->string('car_num');
        $table->string('available_seats');
    });
}

Then my next table...

// Up function for rides table
public function up()
{
    Schema::create('rides', function(Blueprint $table)
    {
        // Explicitly state storage engine
        $table->engine = 'InnoDB';

        // Primary Key
        $table->increments('id')->unsigned();

        // Other columns
        $table->boolean('completed')->default(0);
        $table->boolean('no_show')->default(0);

        // Indexes to foreign keys
        $table->integer('car_id')->unsigned()->index();
   });
}

After all my tables are created, I add my foreign keys to the tables.

public function up()
{
    Schema::table('rides', function(Blueprint $table) 
    {
        $table->foreign('car_id')
              ->references('id')->on('car')
              ->onDelete('cascade');
    });
}

Any advice on how to fix this would be appreciated.

jerney

If anyone in the future is having this issue, I managed to bypass this issue by dropping all of my foreign key restraints. While I still have foreign key indexes that refer to other tables, as far as my mySQL database is concerned, this is just field that holds an unsigned integer. I enforce the relationships between my models in my application like this, and it has worked for me so far.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to define foreign keys in migrations?

From Dev

How to define foreign keys in migrations?

From Dev

Writing Migrations with Foreign Keys Using SequelizeJS

From Dev

Laravel migration foreign keys

From Dev

Cannot add foreign key constraint in Laravel Migrations

From Dev

Laravel eloquent relationships with foreign keys

From Dev

Save object with foreign keys in laravel

From Dev

Laravel 8 - Factories & Foreign Keys

From Dev

"ef migrations add" always recreates foreign keys in the new migration

From Dev

Laravel migration foreign key issue

From Dev

Laravel migration foreign key issue

From Dev

Laravel foreign key saves issue

From Dev

Issue with foreign keys in sql data modeler

From Dev

Laravel migrations nice way of disabling foreign key checks

From Dev

Foreign key constraint error when refreshing migrations - Laravel

From Dev

Insert Laravel model with multiple foreign keys

From Dev

Laravel integer v biginteger in foreign keys

From Dev

Laravel Foreign Keys Drop-down List

From Dev

two foreign keys, how to map with laravel eloquent

From Dev

Retrieving data with foreign keys laravel react

From Dev

Laravel Foreign Keys Drop-down List

From Dev

Symfony2 tests: Issue with fixtures and foreign keys

From Dev

django "use_natural_foreign_keys=True" issue

From Dev

Django update MySQL database issue involving foreign keys

From Dev

How to query pivot table with multiple foreign keys Laravel 5 Eloquent

From Dev

Eloquent relation setup, two foreign keys to same table laravel 5.2

From Dev

How to return data from 2 tables with foreign keys in laravel

From Dev

Laravel relationship between two tables with two foreign keys

From Dev

Laravel relationship, one database has two foreign keys

Related Related

  1. 1

    How to define foreign keys in migrations?

  2. 2

    How to define foreign keys in migrations?

  3. 3

    Writing Migrations with Foreign Keys Using SequelizeJS

  4. 4

    Laravel migration foreign keys

  5. 5

    Cannot add foreign key constraint in Laravel Migrations

  6. 6

    Laravel eloquent relationships with foreign keys

  7. 7

    Save object with foreign keys in laravel

  8. 8

    Laravel 8 - Factories & Foreign Keys

  9. 9

    "ef migrations add" always recreates foreign keys in the new migration

  10. 10

    Laravel migration foreign key issue

  11. 11

    Laravel migration foreign key issue

  12. 12

    Laravel foreign key saves issue

  13. 13

    Issue with foreign keys in sql data modeler

  14. 14

    Laravel migrations nice way of disabling foreign key checks

  15. 15

    Foreign key constraint error when refreshing migrations - Laravel

  16. 16

    Insert Laravel model with multiple foreign keys

  17. 17

    Laravel integer v biginteger in foreign keys

  18. 18

    Laravel Foreign Keys Drop-down List

  19. 19

    two foreign keys, how to map with laravel eloquent

  20. 20

    Retrieving data with foreign keys laravel react

  21. 21

    Laravel Foreign Keys Drop-down List

  22. 22

    Symfony2 tests: Issue with fixtures and foreign keys

  23. 23

    django "use_natural_foreign_keys=True" issue

  24. 24

    Django update MySQL database issue involving foreign keys

  25. 25

    How to query pivot table with multiple foreign keys Laravel 5 Eloquent

  26. 26

    Eloquent relation setup, two foreign keys to same table laravel 5.2

  27. 27

    How to return data from 2 tables with foreign keys in laravel

  28. 28

    Laravel relationship between two tables with two foreign keys

  29. 29

    Laravel relationship, one database has two foreign keys

HotTag

Archive