Skip to content

Can't sync tables that has columns with #[sea_orm(unique)] #2970

@Legend-Master

Description

@Legend-Master

Description

When using entity first workflow, I can no longer use sync if one of the column in the table is marked #[sea_orm(unique)] after upgrading to sea-orm-2.0.0-rc.34, it tries to drop the index for some reason

Looking at the recent changes, I believe these commits might be related

0c7f8ea
1bb4c12

From debugging, it seems like we no longer push unique columns to EntitySchemaInfo::indexes so the diff code will now tries to remove the index

if let Some(existing_table) = existing_table {
// find all unique keys from existing table
// if it no longer exist in new schema, drop it
for existing_index in existing_table.get_indexes() {
if existing_index.is_unique_key() {
let mut has_index = false;
for stmt in self.indexes.iter() {
if existing_index.get_index_spec().get_column_names()
== stmt.get_index_spec().get_column_names()
{
has_index = true;
break;
}
}
if !has_index {
if let Some(drop_existing) = existing_index.get_index_spec().get_name() {
db.execute(sea_query::Index::drop().name(drop_existing))
.await?;
}
}
}
}
}

I have only tested with Postgresql but I assume it would be the same for the other backends

Steps to Reproduce

test.rs

use sea_orm::entity::prelude::*;

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "test_table")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(unique)]
    pub name: String,
}

impl ActiveModelBehavior for ActiveModel {}
db.get_schema_builder()
    .register(test::Entity)
    .sync(&db)
    .await?;

Expected Behavior

No error

Actual Behavior

Exec(SqlxError(Database(PgDatabaseError { severity: Error, code: "2BP01", message: "cannot drop index test_table_name_key because constraint test_table_name_key on table test_table requires it", detail: None, hint: Some("You can drop constraint test_table_name_key on table test_table instead."), position: None, where: None, schema: None, table: None, column: None, data_type: None, constraint: None, file: Some("dependency.c"), line: Some(791), routine: Some("findDependentObjects") })))

Reproduces How Often

Every time after the first run (the run creating the table is fine)

Workarounds

Downgrade to sea-orm-2.0.0-rc.32

Versions

sea-orm-2.0.0-rc.34

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions