On 11/18/2016 11:25 AM, Alexander O'Donovan-Jones wrote:
That's a cool idea, but it would need to reference the instance we've
created to use the `id` atribute.

I was almost going to ask if that's what you meant, but you didn't specify that in your question. Simon's answer contains the adjustment you'd need.


ie the sql would be `select max(version)+1 from responses where id = :id`

On Friday, 18 November 2016 14:39:52 UTC, Mike Bayer wrote:



    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
    <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.

--
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 [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to