user4790312
user4790312

Reputation:

Laravel create simple relationship for 2 table from single table

I have 3 models: User , ChangeMoney and CurrencyType, I have simple relationship with User and ChangeMoney and I want to add another relationship with ChangeMoney table with CurrencyType, but I get this error:

  D:\xampp\htdocs\epay-pro>php artisan migrate

  [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
  : alter table `change_money` add constraint change_money_currency_id_foreig
  n foreign key (`currency_id`) references `currency_type` (`id`))

current my migration is:

Schema::create('change_money',function(Blueprint $table){
    $table->increments('id');
    $table->tinyInteger('currency_type');
    $table->string('current_money');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->integer('currency');
    $table->timestamps();
});

and I don't have any problem, now I want to add other foreign key to CurrencyType table such as:

Schema::create('change_money',function(Blueprint $table){
    $table->increments('id');
    $table->tinyInteger('currency_type');
    $table->string('current_money');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->integer('currency_id')->unsigned();
    $table->foreign('currency_id')->references('id')->on('currency_type');
    $table->timestamps();
});

currency_type table migration:

Schema::create('currency_type',function(Blueprint $table){
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->string('currency_type','50');
    $table->char('currency_symbol','1');
    $table->timestamps();
});

Upvotes: 1

Views: 92

Answers (3)

user4790312
user4790312

Reputation:

PROBLEM SOLVED by split foreign key to other migration

class ForeignKeyBetweenChangeMoneyAndCurrencyType extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('change_money', function (Blueprint $table) {
            $table->integer('currency_id')->unsigned();
            $table->foreign('currency_id')->references('id')->on('currency_type');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('change_money', function(Blueprint $table) {
            $table->dropForeign(' change_money_currency_id_foreign');
        });
    }
}

Upvotes: 0

Daniel Velázquez
Daniel Velázquez

Reputation: 51

Migrations in Laravel runs in order. I don't now your migrations order, but I think that your change_money migration is running first than your currency_type migration. So when the aplication try to add a foreign key in ChangeMoney table to CurrencyType table, the CurrencyType table not exist yet.

Try to create a new migration to add just the relationship between ChangeMoney table and CurrencyType table

Upvotes: 1

Ján Kyselica
Ján Kyselica

Reputation: 699

You have to remove data from both tables ('change_money', 'currency_type').

Or you can turn off the check of foreign keys. In MySQL, or in the phpmyadmin run this:

SET FOREIGN_KEY_CHECKS=0;

Run migration in Laravel: php artisan migrate

And then again turn on check of foreign keys:

SET FOREIGN_KEY_CHECKS=1;

Upvotes: 0

Related Questions