Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a default value in sqlalchemy

People also ask

What is Server_default in SQLAlchemy?

A client-side SQL expression, a server_default value, and server-side implicit defaults and triggers all have the server generate the default, which then must be fetched by the client if you want to be able to access it in the same SQLAlchemy session.

Are SQLAlchemy columns nullable by default?

Columns are nullable by default The default value of SQLAlchemy nullable is False unless it's a primary key. A foreign key is also nullable by default.

What does First () do in SQLAlchemy?

first() applies a limit of one within the generated SQL, so that only one primary entity row is generated on the server side (note this may consist of multiple result rows if join-loaded collections are present).


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:

from sqlalchemy.sql import select, func
...
Column('version', Integer, default=select([func.max(1,
    func.max(version_table.c.old_versions))]))

You want server_default

Column('version', Integer, server_default="SELECT MAX(1, MAX(old_versions)) FROM version_table")