1

I'm creating my database tables inside a flask application via SQLAlchemy. According to this answer, my code should look like this:

id = db.Column(db.CHAR(36), primary_key=True, server_default=str("uuid_generate_v4()"))

which, after migrations and upgrades are done, its definition on the database looks like this:

 id character(36) COLLATE pg_catalog."default" NOT NULL DEFAULT 'uuid_generate_v4()'::bpchar,

Is this the correct way of specifying a UUID default value in PSQL?

4
  • 1
    Your datatype should be UUID, not CHAR(36). Have a look at the uuid-ossp extension. That's assuming SQLAlchemy support the UUID datatype of course.
    – bma
    Commented Nov 14, 2017 at 21:58
  • 1
    Also I'm not sure about the str(..). I think it should be server_default=sqlalchemy.text("uuid_generate_v4()") or just server_default="uuid_generate_v4()" Commented Nov 14, 2017 at 22:00
  • yea, I agree with @ypercubeᵀᴹ. I think there is a problem with that. Commented Nov 14, 2017 at 22:20
  • Yes, the server_default=sqlalchemy.text(...) is needed. Otherwise sqlalchemy thinks you want to have a string constant as the default. Commented Nov 14, 2017 at 22:34

0

Your Answer

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

Browse other questions tagged or ask your own question.