Skip to content

Commit 6e513c6

Browse files
author
platipusica
committed
Fk done
1 parent 23a608a commit 6e513c6

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

utils/scaffold_sqlite3.py

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
import sqlite3
24
import re
35
import json
@@ -374,8 +376,6 @@ def my_database_procedure(db_info):
374376
col_type = col['col_type']
375377
pk = col['pk']
376378

377-
foreign_keys = get_foreign_keys(db_path, table_name)
378-
fk_map = {fk['from']: fk for fk in foreign_keys}
379379

380380
f_field_name = sanitize_field_name(to_camel_case(col_name))
381381
f_name = to_caption(col_name)
@@ -395,6 +395,29 @@ def my_database_procedure(db_info):
395395
f_alignment, F_TEXTAREA, F_DO_NOT_SANITIZE, F_CALC_LOOKUP_FIELD
396396
))
397397

398+
foreign_keys = get_foreign_keys(db_file, table_name)
399+
fk_map = {fk['from']: fk for fk in foreign_keys}
400+
fk = fk_map.get(col['col_name'])
401+
f_lookup_item = None
402+
f_lookup_key = None
403+
f_lookup_result = None
404+
405+
if fk:
406+
ref_table = fk['to_table']
407+
ref_item_id = table_to_item_id.get(ref_table)
408+
if ref_item_id:
409+
f_lookup_item = ref_item_id
410+
f_lookup_key = fk['to_column']
411+
# Try to guess display column: prefer 'name', else first text field
412+
ref_info = get_table_info(db_file, ref_table)
413+
for ref_col in ref_info['fields']:
414+
if ref_col['col_name'].lower() in ('name', 'title', 'description'):
415+
f_lookup_result = ref_col['col_name']
416+
break
417+
if not f_lookup_result:
418+
f_lookup_result = ref_info['fields'][0]['col_name']
419+
print(f"🔗 Lookup detected: {table_name}.{col['col_name']}{ref_table}.{f_lookup_key} (display {f_lookup_result})")
420+
398421
if pk and not pk_detected:
399422
item_id_to_pk_field_id[table_item_id] = field_id
400423
pk_detected = True
@@ -420,6 +443,77 @@ def my_database_procedure(db_info):
420443
cursor.execute("UPDATE SYS_ITEMS SET f_primary_key = ? WHERE id = ?", (pk_field_id, item_id))
421444
print(f"[SYS_ITEMS] Updated item_id={item_id} with f_primary_key={pk_field_id}")
422445

446+
# === STEP 6: Update SYS_FIELDS.F_MASTER_FIELD based on foreign keys ===
447+
448+
# def get_foreign_keys(db_file, table_name):
449+
# cur = conn.cursor()
450+
# cur.execute(f"PRAGMA foreign_key_list('{table_name}')")
451+
# print([
452+
# {"from": r[3], "to_table": r[2], "to_column": r[4]}
453+
# for r in cur.fetchall()
454+
# ])
455+
#
456+
# return [
457+
# {"from": r[3], "to_table": r[2], "to_column": r[4]}
458+
# for r in cur.fetchall()
459+
# ]
460+
461+
print("\n=== STEP 6: Linking fields via F_OBJECT and F_OBJECT_FIELD (foreign keys) ===")
462+
463+
# def get_foreign_keys(db_file, table_name):
464+
# cur = conn.cursor()
465+
# cur.execute(f"PRAGMA foreign_key_list('{table_name}')")
466+
# return [{"from": r[3], "to_table": r[2], "to_column": r[4]} for r in cur.fetchall()]
467+
#
468+
for table_name, item_id in table_to_item_id.items():
469+
fks = get_foreign_keys(db_file, table_name)
470+
if not fks:
471+
continue
472+
473+
print(f"→ Table {table_name} has {len(fks)} foreign keys")
474+
475+
# Cache all fields for this table (lowercase names)
476+
cursor.execute("SELECT id, lower(f_db_field_name) FROM SYS_FIELDS WHERE owner_rec_id = ?", (item_id,))
477+
src_fields = {name: fid for fid, name in cursor.fetchall()}
478+
479+
for fk in fks:
480+
from_field = fk["from"].lower()
481+
ref_table = fk["to_table"].lower()
482+
ref_column = fk["to_column"].lower()
483+
484+
src_id = src_fields.get(from_field)
485+
ref_item_id = None
486+
ref_field_id = None
487+
488+
# Find referenced SYS_ITEMS.id
489+
for tname, iid in table_to_item_id.items():
490+
if tname.lower() == ref_table:
491+
ref_item_id = iid
492+
break
493+
494+
# Find referenced SYS_FIELDS.id
495+
if ref_item_id:
496+
cursor.execute(
497+
"SELECT id FROM SYS_FIELDS WHERE owner_rec_id = ? AND lower(f_db_field_name) = ?",
498+
(ref_item_id, ref_column)
499+
)
500+
row = cursor.fetchone()
501+
if row:
502+
ref_field_id = row[0]
503+
504+
if src_id and ref_item_id and ref_field_id:
505+
cursor.execute(
506+
"UPDATE SYS_FIELDS SET F_OBJECT = ?, F_OBJECT_FIELD = ? WHERE id = ?",
507+
(ref_item_id, ref_field_id, src_id)
508+
)
509+
print(f" ✅ {table_name}.{fk['from']}{fk['to_table']}.{fk['to_column']} | F_OBJECT={ref_item_id}, F_OBJECT_FIELD={ref_field_id}")
510+
else:
511+
print(f" ⚠️ Skipped {table_name}.{fk['from']}{fk['to_table']}.{fk['to_column']} (missing mapping)")
512+
513+
conn.commit()
514+
print("✅ Step 6 complete — F_OBJECT and F_OBJECT_FIELD relationships set.")
515+
516+
423517
# === Finalize ===
424518
conn.commit()
425519
conn.close()

0 commit comments

Comments
 (0)