Problem adding a FOREIGN KEY

0

I am having trouble adding a foreign key to laravel 5.4. Here is the code for migrations below:

Schema::create('anexos_posts', function (Blueprint $table) {
        $table->increments('id');
        $table->string('anexo');
        $table->integer('post_id')->unsigned();
        $table->timestamps();
    });

Then another migration is executed by adding foreign :

Schema::table('anexos_posts', function (Blueprint $table) {
        $table->foreign('post_id')->references('id')->on('postagens');
    });

Error:

  

Schema :: table ('attachments_posts', function (Blueprint $ table) {               $ table-> foreign ('post_id') -> references ('id') -> on ('posts');           });                                                                                                                                                                                                                                                                                               In Connection.php line 647:                                                                                                                                                                             SQLSTATE [HY000]: General error: 1215 Can not add foreign key   constraint (SQL: alter table anexos_posts add constraint    anexos_posts_post_id_foreign foreign key ( post_id ) references    postagens ( id ))

     

In Connection.php line 449:                                                                                SQLSTATE [HY000]: General error: 1215 Can not add foreign key constraint

    
asked by anonymous 11.06.2018 / 20:07

1 answer

0

You can do it all at once. Here is an example of a financial thing I did.

Schema::create('financeiro', function (Blueprint $table) {
        $table->increments('id');
        $table->date('data');
        $table->date('vencimento');
        $table->date('data_pagamento');
        $table->enum('status', array('PROVISIONADO','EFETIVADO'));
        $table->enum('tipo_lancamento', array('CRÉDITO','DÉBITO'));
        $table->integer('pessoa_id')->unsigned();
        $table->integer('user_id')->unsigned()->nullable();
        $table->integer('filial_id')->unsigned()->nullable();
        $table->integer('modalidade_pagamento_id')->unsigned()->nullable();
        $table->integer('bancos_id')->unsigned()->nullable();
        $table->float('valor')->nullable();
        $table->float('descontos')->nullable();
        $table->float('juros')->nullable();
        $table->float('valor_cobrado')->nullable();
        $table->text('historico')->nullable();
        $table->text('observacoes')->nullable();
        $table->foreign('pessoa_id')->references('id')->on('pessoas');
        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('filial_id')->references('id')->on('equipe');
        $table->foreign('modalidade_pagamento_id')->references('id')->on('modalidade_pagamento');
        $table->foreign('bancos_id')->references('id')->on('bancos');
        $table->timestamps();
    });
    
13.06.2018 / 15:11