Skip to content

Commit 1f4d905

Browse files
author
Dencker
committed
Correction when 2 tables have foreign keys to the same table/column
When two tables had foreign keys to the same table and column, the migration would insert the foreign key twice in the same table.
1 parent d429a85 commit 1f4d905

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

export-laravel-5-migrations.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,16 @@ def export_schema(out, schema, is_main_schema):
160160
if item['table'] not in keyed_tables:
161161
keyed_tables.append(item['table'])
162162
if schema_table == 0:
163+
foreign_tbl_name = item['table']
163164
migrations[tbl.name].append('\n')
164165
migrations[tbl.name].append(' Schema::table(\'%s\', function (Blueprint $table) {\n' % (item['table']))
165166
schema_table = 1
167+
elif foreign_tbl_name != item['table']:
168+
foreign_tbl_name = item['table']
169+
migrations[tbl.name].append(" });\n")
170+
migrations[tbl.name].append('\n')
171+
migrations[tbl.name].append(' Schema::table(\'%s\', function (Blueprint $table) {\n' % (item['table']))
172+
166173
migrations[tbl.name].append(' $table->foreign(\'%s\')->references(\'%s\')->on(\'%s\')->onDelete(\'%s\')->onUpdate(\'%s\');\n' % (item['name'], item['referenced_name'], item['referenced_table'], item['delete_rule'].lower(), item['update_rule'].lower()))
167174
if schema_table == 1:
168175
migrations[tbl.name].append(" });\n")

0 commit comments

Comments
 (0)