From d6c803b17bf58c437993704276a7b38dbab1aa0e Mon Sep 17 00:00:00 2001 From: Thanadon Kongkanun <71448810+thanadon-dev@users.noreply.github.com> Date: Fri, 25 Sep 2026 23:38:40 +0700 Subject: [PATCH] fix(sql-schema): omit source sequence default in target DDL --- .../src/sql_dialect/type_rewrite.rs | 9 +++- crates/dbx-sql-schema/src/schema_diff.rs | 50 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/crates/dbx-sql-dialect/src/sql_dialect/type_rewrite.rs b/crates/dbx-sql-dialect/src/sql_dialect/type_rewrite.rs index 11accdf3b1..589eccc7c5 100644 --- a/crates/dbx-sql-dialect/src/sql_dialect/type_rewrite.rs +++ b/crates/dbx-sql-dialect/src/sql_dialect/type_rewrite.rs @@ -435,7 +435,14 @@ pub fn apply_auto_inc_to_column_def( AutoIncColumnBuild::AppendSuffix { suffix, skip_default: false, postgres_sequence: false } } crate::sql_dialect::ddl_profile::AutoIncSyntax::PostgresSequence if col.is_primary_key && is_integer_like => { - AutoIncColumnBuild::AppendSuffix { suffix: "", skip_default: false, postgres_sequence: true } + // The caller emits `ALTER TABLE ... SET DEFAULT nextval('__seq')` + // right after creating that sequence (see `generate_create_table_sql`), so + // the inline default here must be skipped. `col.column_default` is the raw + // value read from the *source* column — typically `nextval('_id_seq' + // ::regclass)` pointing at the source database's own sequence — and inlining + // it fails the CREATE TABLE outright on any target that doesn't already have + // that exact sequence name (#10086: "relation ... does not exist"). + AutoIncColumnBuild::AppendSuffix { suffix: "", skip_default: true, postgres_sequence: true } } _ => AutoIncColumnBuild::Normal { skip_default: false }, } diff --git a/crates/dbx-sql-schema/src/schema_diff.rs b/crates/dbx-sql-schema/src/schema_diff.rs index 6a735ffe76..13e075b167 100644 --- a/crates/dbx-sql-schema/src/schema_diff.rs +++ b/crates/dbx-sql-schema/src/schema_diff.rs @@ -7139,6 +7139,56 @@ mod tests { assert!(sql.contains("INT"), "integer→INT: {sql}"); } + #[test] + fn postgres_create_table_serial_pk_omits_source_sequence_default() { + // #10086: on a Postgres-family target (Kingbase in the report), the source + // column's raw `column_default` is a live `nextval('_id_seq'::regclass)` + // cast pointing at the *source* database's sequence. Inlining that into + // `CREATE TABLE ... DEFAULT nextval(...)` fails at parse time on the target + // because that sequence doesn't exist there yet ("relation ... does not + // exist") — before the table is even created. The correct default is the + // `ALTER TABLE ... SET DEFAULT nextval('__seq')` this function + // already emits further down, against the *new* sequence it just created. + let columns = vec![ColumnDiff { + diff_type: "added".into(), + name: "id".into(), + source: Some(ColumnInfo { + name: "id".into(), + data_type: "bigint".into(), + is_nullable: false, + is_primary_key: true, + column_default: Some("nextval('collection_abnormal_record_id_seq'::regclass)".into()), + ..Default::default() + }), + target: None, + changes: vec![], + add_position: None, + }]; + let (sql, missing) = generate_create_table_sql( + "collection_abnormal_record", + &columns, + &[], + &[], + None, + DatabaseType::Postgres, + None, + Some(DialectKind::Postgres), + &[], + &[], + ); + assert!(missing.is_empty(), "{missing:?}"); + let create_table = sql.split("CREATE SEQUENCE").next().unwrap(); + assert!( + !create_table.contains("nextval"), + "CREATE TABLE must not reference the source's sequence before the new one exists: {sql}" + ); + assert!(sql.contains("CREATE SEQUENCE IF NOT EXISTS"), "new sequence: {sql}"); + assert!( + sql.contains("ALTER TABLE \"collection_abnormal_record\" ALTER COLUMN \"id\" SET DEFAULT nextval("), + "default wired up after the sequence exists: {sql}" + ); + } + #[test] fn sync_to_sqlite_keeps_plain_integer_pk_without_autoincrement() { let columns = vec![ColumnDiff {