Skip to content

fix(sql-schema): stop generating a duplicate "id" column - #95

Open
albertoabellagarcia wants to merge 1 commit into
masterfrom
fix/sql-schema-duplicate-id
Open

fix(sql-schema): stop generating a duplicate "id" column#95
albertoabellagarcia wants to merge 1 commit into
masterfrom
fix/sql-schema-duplicate-id

Conversation

@albertoabellagarcia

Copy link
Copy Markdown
Contributor

Summary

Found while verifying #94 (anyOf support, merged): generate_sql_schema() was generating a duplicate "id" column in effectively every schema it produces.

id properties almost always declare their own "type" (e.g. "type": "string"), so the main type-mapping branch already appends a column for id. Then the unconditional if key == "id": block at the end of the loop appends id again, this time as TEXT PRIMARY KEY -- with no check for whether it was already added above. Every generated schema.sql with an id property ends up with something like:

CREATE TABLE AirQualityObserved ("address" JSON, ..., "id" TEXT, ..., "id" TEXT PRIMARY KEY, ...);

which is invalid SQL -- PostgreSQL rejects CREATE TABLE statements that declare the same column twice.

Fix

Moved the id special-case to the top of the loop, with a continue: id is now handled exactly once and always typed TEXT PRIMARY KEY, which was already the effective outcome (the second append was overriding the first one's type) -- this just stops the first append from happening at all for id.

Testing

  • Verified against AirQualityObserved (the model from generate_sql_schema - support for anyOf #87): "id" now appears exactly once in the generated schema, correctly typed as TEXT PRIMARY KEY.
  • Re-ran generate_sql_schema() against all 231 real model.yaml files available in a local FIWARE GitHub org checkout: no exceptions, and exactly one "id" column for every entity that declares an id property. 3 entities in that set (Distribution, Presentation) don't declare an id property at all and are correctly unaffected.

Independent of #87/#94 -- this bug exists for any model with an id property, regardless of whether it also uses anyOf.

generate_sql_schema() appended a column for "id" twice in every
generated schema: once via the main type-mapping branches (since an
id property almost always declares its own "type", e.g. "string"),
and again via the unconditional `if key == "id":` block at the end
of the loop, which always fires regardless of what happened above.
The result was an invalid CREATE TABLE statement declaring the same
column twice in effectively every generated schema.sql.

Moved the id special-case to the top of the loop with a `continue`,
so it's handled exactly once and always as TEXT PRIMARY KEY,
matching the original intent (id's own declared "type"/"format" was
already being discarded in favor of TEXT PRIMARY KEY -- this just
stops it from also being appended as a second column first).

Verified against AirQualityObserved (the model from #87): "id"
appears exactly once, correctly typed. Re-ran against all 231 real
model.yaml files available locally: no exceptions, and exactly one
"id" column per entity that declares an id property (3 entities in
the set don't declare one at all, correctly unaffected).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant