apgdiff icon indicating copy to clipboard operation
apgdiff copied to clipboard

Changing NULLability produces DDL that Postgresql 9.5 does not understand

Open moritz opened this issue 8 years ago • 0 comments

When I have these two definitions:

CREATE TABLE users (
    id          SERIAL,
    email       VARCHAR NOT NULL,
    last_login  TIMESTAMP,
    PRIMARY KEY(id)
);

and

CREATE TABLE users (
    id          SERIAL,
    email       VARCHAR NOT NULL,
    last_login  TIMESTAMP NOT NULL DEFAULT NOW(),
    PRIMARY KEY(id)
);

Then apgdiff 2.4 produces

ALTER TABLE users
    ALTER COLUMN last_login TYPE TIMESTAMP NOT NULL /* TYPE change - table: users original: TIMESTAMP new: TIMESTAMP NOT NULL */,
    ALTER COLUMN last_login SET DEFAULT NOW();

Which postgresql 9.5 rejects with

ERROR:  syntax error at or near "NOT"
LINE 2: ALTER COLUMN last_login TYPE TIMESTAMP NOT NULL,
                                               ^

It seem that postgres 9.5 doesn't like constraints in an ALTER COLUMN ... TYPE, and the correct output would be

ALTER TABLE users
    ALTER COLUMN last_login SET NOT NULL,
    ALTER COLUMN last_login SET DEFAULT NOW();

instead.

See https://www.postgresql.org/docs/9.5/static/sql-altertable.html for the documentation, which mentinos SET NOT NULL and DROP NOT NULL.

moritz avatar Aug 10 '16 19:08 moritz