Skip to content

[sea-orm-migration] Drop dependant object on fresh operations #3012

@SteelAlloy

Description

@SteelAlloy

Motivation

I should point out that I’m using sea-orm-migration indirectly, via the Loco CLI.

I consistently run into a problem in my codebase where a function takes a Postgres enum as an argument, which means it cannot be reset.

  2026-03-18T13:49:00.537529Z  INFO sea_orm_migration::migrator: Dropping type 'timezone_name'
    at /home/redacted/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/sea-orm-migration-1.1.19/src/migrator.rs:337

  2026-03-18T13:49:00.545024Z  INFO sea_orm_migration::migrator: Type 'timezone_name' has been dropped
    at /home/redacted/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/sea-orm-migration-1.1.19/src/migrator.rs:341

  2026-03-18T13:49:00.545405Z  INFO sea_orm_migration::migrator: Dropping type 'account_kind'
    at /home/redacted/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/sea-orm-migration-1.1.19/src/migrator.rs:337

Error: DB(Exec(SqlxError(Database(PgDatabaseError { severity: Error, code: "2BP01", message: "cannot drop type account_kind because other objects depend on it", detail: Some("function email_alias(character varying,account_kind) depends on type account_kind"), hint: Some("Use DROP ... CASCADE to drop the dependent objects too."), position: None, where: None, schema: None, table: None, column: None, data_type: None, constraint: None, file: Some("dependency.c"), line: Some(1148), routine: Some("reportDependentObjects") }))))

For now, I'm manually dropping the table by connecting to the database, which is a bit tedious.

Proposed Solutions

As Postgres points out, all it takes is to use the cascade mode.

Use DROP ... CASCADE to drop the dependent objects too.
    // Drop all types
    if db_backend == DbBackend::Postgres {
        info!("Dropping all types");
        let stmt = query_pg_types(db);
        let rows = db.query_all(&stmt).await?;
        for row in rows {
            let type_name: String = row.try_get("", "typname")?;
            info!("Dropping type '{}'", type_name);
-           let mut stmt = Type::drop();
+           let mut stmt = Type::drop().cascade();
            stmt.name(Alias::new(&type_name));
            db.execute(&stmt).await?;
            info!("Type '{}' has been dropped", type_name);
        }
    }

Are there reasons for this that I'm not aware of?

Additional Information

I've checked the latest of master, where this is still the case https://github.com/SeaQL/sea-orm/blob/master/sea-orm-migration/src/migrator/exec.rs#L123

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions