1

I have a question about migrations in Laravel. Is it possible to handle foreign keys with a simple if statement? Specifically, I want to ensure that if a foreign key already exists, it should not be migrated again. Is there a straightforward way to do this?

This is my code for example.

        if (Schema::connection('orders_import')->hasTable('mymuesli_label')) {
            Schema::connection('orders_import')->table('mymuesli_label', function (Blueprint $table) {
                $table->foreign(['roll'], 'roll_id')->references(['id'])->on('mymuesli_roll');
            });
        }

        if (Schema::connection('orders_import')->hasTable('parameter_mapping')) {
            Schema::connection('orders_import')->table('parameter_mapping', function (Blueprint $table) {
                $table->foreign(['customer'], 'parameter_mapping_fk_customer')->references(['id_customer'])->on('customer');
            });
        }

        if (Schema::connection('orders_import')->hasTable('product_mapping')) {
            Schema::connection('orders_import')->table('product_mapping', function (Blueprint $table) {
                $table->foreign(['customer'], 'product_mapping_fk_customer')->references(['id_customer'])->on('customer');
            });
        }
0

2 Answers 2

2

I think this will be the straightforward method with an if statement. Here is your updated migration code.

    if (Schema::connection('orders_import')->hasTable('parameter_mapping')) {
        Schema::connection('orders_import')->table('parameter_mapping', function (Blueprint $table) {
            if (!Schema::hasColumn('parameter_mapping','parameter_mapping_fk_customer')) {
                $table->foreign(['customer'], 'parameter_mapping_fk_customer')->references(['id_customer'])->on('customer');
            }
        });
    }

    if (Schema::connection('orders_import')->hasTable('product_mapping')) {
        Schema::connection('orders_import')->table('product_mapping', function (Blueprint $table) {
            if (!Schema::hasColumn('product_mapping','product_mapping_fk_customer')) {
                $table->foreign(['customer'], 'product_mapping_fk_customer')->references(['id_customer'])->on('customer');
            }
        });
    }

Just use If(!Schema->hasColumn('table_name', 'column_name'))

This method has two parameters, hasColumn('table_name', 'column_name')

Sign up to request clarification or add additional context in comments.

Comments

1

I believe yes, but with laravel 11. With Schema::getForeignKeys('your_table'). I will do an example for your first code:

use Illuminate\Support\Facades\Schema;

$foreignKeys = Schema::connection('orders_import')->getForeignKeys('mymuesli_label');
$foreignKeyExists = false;

foreach ($foreignKeys as $foreignKey) {
    if ($foreignKey['name'] === 'roll_id') {
        $foreignKeyExists = true;
        break;
    }
}

if (!$foreignKeyExists) {
    Schema::connection('orders_import')->table('mymuesli_label', function (Blueprint $table) {
        $table->foreign(['roll'], 'roll_id')->references(['id'])->on('mymuesli_roll');
    });
}

// Then set the $foreignKeyExists to false and keep with the search on your tables.

ps: This method gives you an array of arrays with name (string) and columns (array of strings), and you need to get it right with the migration ->foreign($columns, $name).

1 Comment

Sorry, I might have missed mentioning that there could potentially be around 700 instances where this issue might occur. I generated new migrations using this package github.com/kitloong/laravel-migrations-generator for a very large project. On localhost, it can unfortunately happen that there are problems with foreign keys where a foreign key might already exist. I need a solution that can handle this entire block of code with an if statement without individually listing the foreign keys, but I'm not sure if that's possible. @francisco

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.