typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
34.14k stars 6.29k forks source link

[Migration Generator]: Error when updating view-expression of view that has dependent objects #8100

Closed jonahlk closed 2 years ago

jonahlk commented 3 years ago

Issue Description:

[Migration Generator]: Error when updating view-expression of view that has dependent objects

Expected Behavior

When updating a view, it should drop all dependent views in the right order before it drops and recreates the actual view that should be changed and rebuild them again afterwards.

Actual Behavior

Only the view thats beeing edited is beeing dropped, which results in an error because there are dependent objects:

QueryFailedError: cannot drop view demo_view_entity because other objects depend on it

Steps to Reproduce

Please checkout and run this demo repo: https://github.com/jonahlk/typeorm-view-bug

This will create 4 Entities: 2 tables and 2 views.

If you update the expression of the DemoViewEntity to:

@ViewEntity({
  expression: `
      SELECT
          DEO.id                  AS id,
          DEO.property            AS property,
          DET.some_other_property AS some_other_property,
          NULL                    AS new_column,
          NULL                    AS new_column_2
      FROM
          demo_entity_one DEO
              LEFT JOIN
              demo_entity_two DET ON DET.demo_entity_one_id = DEO.id
  `,
})

and run the typeorm schema:log you will see, that only the DemoViewEntity will be dropped, which will result in an error cause of the dependent view.

This would be the expected behavior:

  1. Drop DemoViewEntityChild
  2. Drop DemoViewEntity
  3. Recreate DemoViewEntity
  4. Recreate DemoViewEntityChild

My Environment

Dependency Version
Operating System
Node.js version ^14
Typescript version 4.3.5
TypeORM version 0.2.37

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

Ginden commented 2 years ago

You need to define dependsOn.