Karthikeyan KR
Karthikeyan KR

Reputation: 1174

Stop/Remove Inserting NULL as default in SQLAlchemy

I have the following columns in SQLAlchemy:

name = Column(String(36))
uuid= Column(String(36))

Sample Insertion query:

new_request = Project(name = names)
db_session.add(new_request)
db_session.commit()

I already set default value for uuid in database.

But SQLAlchemy set uuid=None in query, so default value is not generating.

How to avoid default null insertion.

Upvotes: 1

Views: 197

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

One of possible ways is using builtin UUIDType:

your_column = Column(UUIDType(binary=False), primary_key=True)

A custom uuid generators are also acceptable via default=... and server_default=... arguments of Column type.

With server_default=... you are able to specify a UUID generator function typical for a certain database engine (like gen_random_uuid() for PostgreSQL).

Upvotes: 2

Related Questions