0

I'm trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint `users_ent_id_foreign` foreign key (`ent_id`) references `id_ent` (`entreprises`))

My migration code for 'Entreprises'

 public function up()
{
    Schema::create('entreprises', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id_ent');
        $table->string('name');
        $table->string('numSiret', 14)->unique();
        $table->integer('nbr_couvert')->length(10);
        $table->timestamps();
    });
}

My migration code for 'Users'


    public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id_user');
        $table->string('log');
        $table->string('name', 45);
        $table->string('firstName', 45);
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->string('num_phone', 20);
        $table->datetime('created_at');
        $table->unsignedInteger('ent_id');
        $table->integer('agenda_id')->nullable();
        $table->tinyInteger('droitUser')->nullable();
        $table->boolean('validateMail');
        $table->string('key', 255);
    });

    Schema::table('users', function ($table) {
        $table->foreign('ent_id')
            ->references('entreprises') //here 
            ->on('id_ent'); //and here
    });

I change the order of the migrations and the table 'entreprises' are create before the 'users' table.

I tried every solutions from here and here and they are nothing to change.

Any suggestions someone ?

Editing for solution

Shame on me... I just do a stupid mistake when I reference the table I wanted to reference... Everything is fine now... Sorry

Jo Le Belge
  • 137
  • 1
  • 2
  • 14
  • It's not similar than https://stackoverflow.com/questions/1457305/mysql-creating-tables-with-foreign-keys-giving-errno-150 ... – Jo Le Belge Feb 05 '20 at 02:20

2 Answers2

0

Are you using sqlite database? if so sqlite database by default disabled foreign key feature.

SQLite disables foreign key constraints by default. When using SQLite, make sure to enable foreign key support in your database configuration before attempting to create them in your migrations.

you can check here to enable foreign key in sqlite database.

Alcyone
  • 15
  • 5
  • Ok you missing Blueprint::class inside callback function parameter Schema::table – Alcyone Feb 05 '20 at 05:00
  • That change absolutly...nothing... And it's strange because if I delete the ligne for add foreign constraint, I ran migration and I add foreign Key manually in my phpMyAdmin, I have no problem for do it... – Jo Le Belge Feb 05 '20 at 07:40
0
Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->unsigned();
            $table->string('title');
            $table->string('slug')->unique();
            $table->string('image')->default('default.png');
            $table->string('zip');
            $table->text('body');
            $table->integer('view_count')->default(0);
            $table->boolean('status')->default(false);
            $table->boolean('is_approved')->default(false);
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();