Skip to content

Load reference table model before creating a reference table - #57

Merged
fcollman merged 1 commit into
masterfrom
fix-reference-table-cold-metadata
Nov 26, 2026
Merged

fcollman merged 1 commit into
masterfrom
fix-reference-table-cold-metadata

Conversation

@fcollman

Copy link
Copy Markdown
Collaborator

Fixes #56.

create_table compiled the new table's DDL without making sure the target table's model was registered in the process-local Base.metadata. The reference column's ForeignKey("<target>.id") is a string resolved against that metadata, so creation failed with NoReferencedTableError whenever the serving worker had not already used the target table. That is why it appeared intermittent across uwsgi workers.

Fix: if the target is not in Base.metadata.tables, load its model via _get_model_from_table_name before building the new model. This checks the metadata directly rather than cached_table(), which can be out of step with it.

Test: test_create_reference_table_cold_metadata removes the target from the metadata and cache before creating the reference table. It fails without the fix with the same error as the issue, and passes with it. Full suite: 57 passed against PostGIS.

Not addressed: Base.metadata is keyed by table name only, so same-named tables in different aligned volumes can shadow each other (noted in the issue as a separate problem).

🤖 Generated with Claude Code

create_table compiled the new table's DDL without ensuring the target
table's model was registered in the in-process Base.metadata, so the
string ForeignKey failed with NoReferencedTableError whenever the worker
had not already used the target table. Load the target model first, and
add a regression test with the target absent from the metadata.

Fixes #56

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@fcollman
fcollman merged commit 6aa3218 into master Nov 26, 2026
1 check passed
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.

Creating reference tables fails.

1 participant