70

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

Currently I have this:

Column('version', Integer, default=1)

What I need is (roughly) this:

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

How can I implement this in SQLAlchemy?

3 Answers 3

63

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))]))
6
  • 1
    I'm aware of the abilities but for some reason I can't seem to formulate the right query. I'd love some specifics, if you can offer them.
    – Chris R
    Mar 14, 2012 at 17:14
  • @ChrisR: Updated the answer with an example.
    – voithos
    Mar 14, 2012 at 17:22
  • That's really close... but in the case where there is no record in version_table max() returns NULL.
    – Chris R
    Mar 14, 2012 at 19:32
  • 2
    @ChrisR: How about using func.coalesce? Something like func.coalesce(func.max(version_table...), --Default Value Here--)? I haven't tested this code, however.
    – voithos
    Mar 14, 2012 at 19:36
  • 1
    Note this creates a client-side default instead of RDBMS default in the database. See my answer.
    – markmnl
    Oct 30, 2017 at 3:50
19

You want server_default

Column('version', Integer, server_default="SELECT MAX(1, MAX(old_versions)) FROM version_table")
1
  • 2
    is there a way to modify this to filter on a value being used as part of the insert? for example, select max(...) from table where table.some_field=some_value? some_value is one of the values being inserted. EDIT: sadly it looks like no, postgres doesn't allow this: "The value is any variable-free expression (in particular, cross-references to other columns in the current table are not allowed). Subqueries are not allowed either."
    – David
    Feb 6, 2020 at 19:09
0

If you want to use a DML statement to generate the default value, you can simply use the text method to indicate that you are passing DML. You may also need an extra set of parentheses if the engine wants to write this inside a VALUES clause , e.g.:

from sqlachemy import text
Column('version', Integer, default=text("(SELECT MAX(1, MAX(old_versions)) FROM version_table)"))

I've used this technique to use a sequence to override the server default ID generation, e.g.:

Column('version', Integer, default=text("NEXT VALUE FOR someSequence"))

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.