How to prevent UUID primary key for new SQLAlchemy objects being created with the same value

The sqlalchemy docs for Column’s default argument state:

A scalar, Python callable, or ColumnElement expression representing the default value for this column, which will be invoked upon insert if this column is otherwise not specified in the VALUES clause of the insert.

So rather than providing uuid.uuid4(), which will create a scalar(constant) value, provide only the callable uuid.uuid4, so that it is invoked for each insert.

There is further discussion of defaulting values in Column Insert/Update Defaults

Leave a Comment