Skip to content

Commit

Permalink
Fixed a bug in PostgreSQL when dropping NOT NULL constraints. (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
uehara-delta committed Feb 23, 2023
1 parent 5af7273 commit b67e86c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/SQL/Translator/Producer/PostgreSQL.pm
Expand Up @@ -1058,6 +1058,11 @@ sub alter_drop_constraint {
my ($c, $options) = @_;
my $generator = _generator($options);

# NOT NULL constraint does not require a DROP CONSTRAINT statement
if ( $c->type eq NOT_NULL) {
return;
}

# attention: Postgres has a very special naming structure for naming
# foreign keys and primary keys. It names them using the name of the
# table as prefix and fkey or pkey as suffix, concatenated by an underscore
Expand Down
4 changes: 4 additions & 0 deletions t/30sqlt-new-diff-pgsql.t
Expand Up @@ -78,6 +78,8 @@ ALTER TABLE "person" ALTER COLUMN "name" SET NOT NULL;
ALTER TABLE "person" ALTER COLUMN "age" SET DEFAULT 18;
ALTER TABLE "person" ALTER COLUMN "weight" DROP NOT NULL;
ALTER TABLE "person" ALTER COLUMN "iq" TYPE bigint;
ALTER TABLE "person" ALTER COLUMN "nickname" SET NOT NULL;
Expand Down Expand Up @@ -133,6 +135,8 @@ ALTER TABLE person ALTER COLUMN name SET NOT NULL;
ALTER TABLE person ALTER COLUMN age SET DEFAULT 18;
ALTER TABLE person ALTER COLUMN weight DROP NOT NULL;
ALTER TABLE person ALTER COLUMN iq TYPE bigint;
ALTER TABLE person ALTER COLUMN nickname SET NOT NULL;
Expand Down
14 changes: 13 additions & 1 deletion t/data/diff/pgsql/create1.yml
Expand Up @@ -135,6 +135,18 @@ schema:
reference_fields: []
reference_table: ''
type: UNIQUE
- deferrable: 1
expression: ''
fields:
- weight
match_type: ''
name: ''
on_delete: ''
on_update: ''
options: []
reference_fields: []
reference_table: ''
type: NOT_NULL
fields:
age:
data_type: int
Expand Down Expand Up @@ -194,7 +206,7 @@ schema:
data_type: numeric
default_value: ~
extra: {}
is_nullable: 1
is_nullable: 0
is_primary_key: 0
is_unique: 0
name: weight
Expand Down

0 comments on commit b67e86c

Please sign in to comment.