Views referencing each other can't be created simultaneously
See original GitHub issueI can’t seem to create multiple views, where one references a previous view, at the same time.
An minimal example of what I would like to define (at the same time, so both views should be generated within one migration):
first_view = PGView(
schema="public",
signature="first_view",
definition="SELECT * FROM some_table;"
)
second_view = PGView(
schema="public",
signature="second_view",
definition="SELECT * FROM first_view;"
)
I get the following error while trying to autogenerate the alembic migration: psycopg2.errors.UndefinedTable: relation "second_view" does not exist
.
It seems that while comparing the existing views with the newly defined views, alembic actually attempts to execute the view definition, which fails for the second view as the first view is then not yet defined.
https://github.com/olirice/alembic_utils/blob/a4bc7f5f025335faad7b178eb84ab78093e525ec/src/alembic_utils/replaceable_entity.py#L28-L40
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
CREATE VIEW SQL: Modifying views in SQL Server
This is the SQL Server way of saying that we have referenced more than one column with the same name in the FROM...
Read more >In SQL, is it OK for two tables to refer to each other?
No, it's not OK. Circular references between tables are messy. See this (decade old) article: SQL By Design: The Circular Reference.
Read more >MySQL 5.6 Reference Manual :: 20.9 Restrictions on Views
Subqueries cannot be used in the FROM clause of a view. There is a general principle that you cannot modify a table and...
Read more >CREATE VIEW (Transact-SQL) - SQL Server - Microsoft Learn
All referenced objects must be in the same database. Views or tables that participate in a view created with the SCHEMABINDING clause cannot...
Read more >Creating multiple tables and table relationships - Launch School
In other words, PostgreSQL won't allow you to add a value to the Foreign Key column of a table if the Primary Key...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Resolved in
alembic_utils>=0.3.2
Tested here Not all cases are covered but general use is expected to work
If you end up on this thread and are running into an problem please open a new issue with minimal reproducible example.
Good point, I didn’t consider the rendering step.
At the moment, the only solution would be to manually write the migration as shown ^, or autogenerate a new migration when you want to checkpoint and be able to refer to the new entities like:
first_view
to sourcefirst_view
second_view
to sourcesecond_view