Skip to content

Commit b2316ba

Browse files
authored
Merge pull request #8 from edencker/patch-1
Foreign Keys problems
2 parents 51b590a + 1f4d905 commit b2316ba

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

export-laravel-5-migrations.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def export_schema(out, schema, is_main_schema):
3434
migration_tables = []
3535
global migrations
3636

37-
for tbl in schema.tables:
37+
for tbl in sorted(schema.tables, key=lambda table: table.name):
3838
migration_tables.append(tbl.name)
3939
migrations[tbl.name] = []
4040
migrations[tbl.name].append('<?php\n')
@@ -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)