Laravel migration not accepting foreign key

时间:2016-08-31 18:30:54

标签: php sql laravel

I have a bd model and I made a migation for it. I have Feedback, Oraganization and Carrers.

Organization have many carrers and Feedback has 1 carrer.

That's my migration:

//Carrers Table
    public function up()
        {
            Schema::create('Careers', function (Blueprint $table) {
                $table->integer('idCareers')->unsigned();
                $table->string('name', 45)->nullable();
                $table->float('salary')->nullable();
                $table->string('degree', 45)->nullable();
                $table->integer('Organization_idOrganization')->unsigned();
            });
        }

        public function down()
        {
            Schema::drop('Careers');
        }
//Feedback Table
    public function up()
        {
            Schema::create('Feedback', function (Blueprint $table) {
                $table->integer('idFeedback')->unsigned();
                $table->float('parameter_m')->nullable();
                $table->float('parameter_alpha')->nullable();
                $table->float('parameter_beta')->nullable();
                $table->float('parameter_pu')->nullable();
                $table->integer('Careers_idCareers')->unsigned();
                $table->integer('Careers_Organization_idOrganization')->unsigned();

                $table->foreign('Careers_idCareers')->references('idCareers')->on('Careers')->onDelete('no action')->onUpdate('no action');
            });
        }

        public function down()
        {
            Schema::table('Feedback', function (Blueprint $table) {
                $table->dropForeign(['Careers_idCareers']);
            });

            Schema::drop('Feedback');
        }
//Organization table
    public function up()
        {
            Schema::create('Organization', function (Blueprint $table) {
                $table->increments('idOrganization');
                $table->string('name', 45)->nullable();
            });

            Schema::table('Careers', function (Blueprint $table) {
                $table->foreign('Organization_idOrganization')->references('idOrganization')->on('Organization')->onDelete('no action')->onUpdate('no action');
            });

        }

        public function down()
        {
            Schema::table('Careers', function (Blueprint $table) {
                $table->dropForeign(['Organization_idOrganization']);
            });

            Schema::drop('Organization');
        }

But I am getting this error:

General error: 1215 Cannot add foreign key constraint (SQL: alter table `Feedback` add constraint `feedback_careers_idca  
  reers_foreign` foreign key (`Careers_idCareers`) references `Careers` (`idCareers`) on delete no action on update no action) 

Which just happen when I make the 3 relation. When I have just one relation it works fine. Also when I create it from the sql I get no problems.

Any Ideas why is this happening?

0 个答案:

没有答案