On 11/18/2016 09:10 AM, Alexander O'Donovan-Jones wrote:
I'm currently working on using the ORM features of sqlalchemy with a
legacy database table. The table can be roughly described like this:

    class APIResponse(Base):
        __tablename__ = 'responses'
        id = Column(Text, primary_key=True)
        version = Column(Integer, primary_key=True)
        payload = Column(JSONB, nullable=False)
        created_at = Column(DateTime, nullable=False)


The table stores JSON payloads where the primary key is determined by
the combination of the id and version columns (as a composite primary
key). What I'm trying to work out is whether it's possible to have the
value of the version column be the result of
func.max(APIResponse.version) + 1, that is, MAX()+1 to increment the
version.

This is running on Postgresql, and so I took a look at the sequence
support, but that appears to be a table wide sequence, and not keyed off
a value (ie: I couldn't have 10000 id's each mapping to a sequence to
generate versions).


you can set any SQL expression you want for a default, some examples at https://docs.sqlalchemy.org/en/latest/core/defaults.html#sql-expressions. This would look like default=select([func.max(API.response.version) + 1]). hope that helps.






I'm not sure if I've explained myself clearly, but I'm a little stumped
as to how I might achieve this.

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
---
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to