Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,16 @@ def generate_sql_schema(model_yaml: str) -> str:
table_create_statement = f"CREATE TABLE {entity} ("

for key, value in model_yaml[entity]["properties"].items():
# "id" always maps to the primary key column, regardless of what
# "type"/"format"/etc. it declares -- handle it here and move on, so
# it isn't *also* appended by the type-mapping branches below (that
# produced a duplicate "id" column in every generated schema, since
# id properties almost always declare a "type").
if key == "id":
field_type = "TEXT PRIMARY KEY"
sql_schema_statements.append(f"\"{key}\" {field_type}")
continue

field_type = "JSON" # Default to JSON if type is not defined

# Field type mapping
Expand Down Expand Up @@ -1226,11 +1236,6 @@ def generate_sql_schema(model_yaml: str) -> str:
sub_field_type = type_mapping.get(sub_value["type"])
sql_schema_statements.append(f"\"{sub_key}\" {sub_field_type}")

if key == "id":
field_type = "TEXT PRIMARY KEY"
# add attribute to the SQL schema statement
sql_schema_statements.append(f"\"{key}\" {field_type}")

# Complete the CREATE TABLE statement
table_create_statement += ", ".join(sql_schema_statements)
table_create_statement += ");"
Expand Down