Skip to content
Advertisement

Setting a default value in sqlalchemy

I would like to set a column default value that is based on another table in my SQLAlchemy model.

Currently I have this:

JavaScript

What I need is (roughly) this:

JavaScript

How can I implement this in SQLAlchemy?

Advertisement

Answer

The documentation gives the following possibilities for default:

A scalar, Python callable, or ClauseElement 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.

You may look into using a simple function, or you may just be able to use a select() object.

In your case, maybe something along the lines of:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement