question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Views referencing each other can't be created simultaneously

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
oliricecommented, Jan 20, 2021

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.

1reaction
oliricecommented, Sep 16, 2020

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:

  • add first_view to source
  • autogenerate migration for first_view
  • apply the migration
  • add second_view to source
  • autogenerate migration for second_view
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found