From d30df59855c0c8f670668455b34701af073ba10d Mon Sep 17 00:00:00 2001 From: opusbopus Date: Thu, 7 Sep 2023 16:07:53 -0400 Subject: [PATCH] Fix incorrect behavior when adding an autoincrement column to a Postgres table. --- src/backend/postgres/table.rs | 6 +++++- tests/postgres/table.rs | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/backend/postgres/table.rs b/src/backend/postgres/table.rs index e0a8ed854..ab5463f33 100644 --- a/src/backend/postgres/table.rs +++ b/src/backend/postgres/table.rs @@ -112,7 +112,11 @@ impl TableBuilder for PostgresQueryBuilder { let f = |column_def: &ColumnDef, sql: &mut dyn SqlWriter| { if let Some(column_type) = &column_def.types { write!(sql, " ").unwrap(); - self.prepare_column_type(column_type, sql); + if column_def.spec.iter().any(|v| matches!(v, ColumnSpec::AutoIncrement)) { + self.prepare_column_auto_increment(column_type, sql); + } else { + self.prepare_column_type(column_type, sql); + } } }; self.prepare_column_def_common(column, sql, f); diff --git a/tests/postgres/table.rs b/tests/postgres/table.rs index 6292000bc..ece729c32 100644 --- a/tests/postgres/table.rs +++ b/tests/postgres/table.rs @@ -519,7 +519,7 @@ fn alter_9() { #[test] fn alter_10() { - // https://dbfiddle.uk/PQksflGf + // https://dbfiddle.uk/BeiZPvBe assert_eq!( Table::alter() .table(Glyph::Table) @@ -534,7 +534,7 @@ fn alter_10() { .to_string(PostgresQueryBuilder), [ r#"ALTER TABLE "glyph""#, - r#"ADD COLUMN "aspect" integer NOT NULL UNIQUE PRIMARY KEY"#, + r#"ADD COLUMN "aspect" serial NOT NULL UNIQUE PRIMARY KEY"#, ] .join(" ") );