Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a json file containing data needed to drop a foreign key #3

Open
ajcastro opened this issue Oct 2, 2018 · 0 comments
Open

Comments

@ajcastro
Copy link
Owner

ajcastro commented Oct 2, 2018

For example we are developing a blog app. The tables involved are users, tags and posts tables.
Migration Files:
20180101_create_users_table

$table->increments('id');
$table->string('name');

20180101_create_tags_table

$table->increments('id');
$table->string('name');

20180101_create_posts_table

  $table->increments('id');
  Fk::make($table)->add('user_id');
  Fk::make($table)->add('tag_id');
  $table->string('title');
  $table->string('body');

This should generate a json of data that is needed when deleting the foreign key.
Those json files will be saved in directory database/foreign_keys/ and the json filename would be the migration_file_name.json.
So when we run the above migration it will create a file database/foreign_keys/20180101_create_posts_table.json. One json file per migration file. Only 20180101_create_posts_table.json is created because it is the only migration file that calls Fk::make().

The data needed to drop a foreign key is the:

  • table
  • foreign key name.
    Sample Dropping of foreign key :
Schema::table('posts', function ($table) {
  $table->dropForeignKey('the_foreign_key_name');
});

Sample json file format:

[{
"table" : "posts",
"foreign_key_names: ["posts_user_id_foreign_key", "posts_tag_id_foreign_key"]
}]

It is an array of objects, because it can be possible that one migration file can involve many tables.

The code will be placed in the Fk::migrate() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant