2

I'm trying to run a migration and make basically a column "modified" which is NOT NULL to be NULL. Like there is no need to have that constraint on it, I've run yoyo migrations and have the following output

psycopg2.ProgrammingError: syntax error at or near "timestamp"
LINE 1: ALTER TABLE shop ALTER COLUMN modified timestamp NULL

Pointing to the timestamp ^

the table itself looks

CREATE TABLE shop (
    id SERIAL PRIMARY KEY,
    uuid uuid NOT NULL UNIQUE,
    created timestamp with time zone NOT NULL,
    modified timestamp with time zone NOT NULL,
    deleted timestamp with time zone
);

I've tried to search the web, and found a few similar articles on stackoverflow but it didn't help, so hopefully someone here can help.

Edit:

steps = [
    step("""ALTER TABLE phrases 
    ALTER COLUMN modified TYPE timestamp,
    ALTER column modified SET NULL
;""")
]

In yoyo migration

1 Answer 1

2

In Postgres, you can make a column nullable with DROP NOT NULL:

ALTER TABLE shop ALTER column modified DROP NOT NULL;

If you want to change the datatype at the same time, then:

ALTER TABLE shop 
    ALTER column modified DROP NOT NULL,
    ALTER COLUMN modified TYPE timestamp
;

Demo on DB Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

and for the roleback would i do something like ALTER TABLE shop ALTER column modified SET NOT NULL;?
@Stas: yes, that's how you do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.