1

I tried to change the forma but nothing changed. I have this migration tables and I want to link between them one to many relation , but I got an error.

this is the poster(publisher) table:

  Schema::create('posters', function (Blueprint $table) {
            $table->increments('id'); // tha means integer
            $table->string('username');
            $table->string('email');
            $table->timestamps();
        });

this is article table :

 Schema::create('articles',function(Blueprint $table){
            $table->increments('id');
            $table->integer('poster_id')->unsigned(); //it s integer too
            $table->string('name');
            $table->longText("content");

            $table->foreign('poster_id')->references('id')->on('posters')->onDelete("cascade"); 
        });

full error :

SQLSTATE[HY000]: General error: 1005 Can't create table laravel.articles (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table articles add constraint articles_poster_id_foreign foreign key (poster_id) references posters (id) on delete cascade)

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
azdeviz
  • 597
  • 2
  • 9
  • 20
  • ok I will check it now – azdeviz Jul 16 '20 at 09:04
  • @lagbox - I've heard that here before, but I just checked the [docs](https://laravel.com/docs/7.x/migrations#creating-tables) and it actually says: `$table->increments('id'); - Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column.` and then there's a `$table->bigIncrements('id')` for creating bigints. – M. Eriksson Jul 16 '20 at 09:06
  • I have changed the integer to biginteger at the foreign key but nothing changed I got same error – azdeviz Jul 16 '20 at 09:07
  • 1
    just make sure the order of these migrations is running in the order you think, check the timestamp at the beginning of the migration files ... @MagnusEriksson yes you are correct i was thinking of the `id` of Blueprint not `increments` – lagbox Jul 16 '20 at 09:07
  • I think it does not run in the logic that I want when I migrate , the "article" table created before "poster" that means forign key created before the primary key , I will remove them again and try to change the order – azdeviz Jul 16 '20 at 09:13

0 Answers0