You can provide a function for the default value, and the function can receive the current statement context as a parameter. This context gives you access to the rest of the insert statement, including values of other parameters:
http://docs.sqlalchemy.org/en/latest/core/defaults.html#context-sensitive-default-functions I haven't tested this at all, but perhaps you could use something like: def getdefaultid(context): return (select([func.max(APIResponse.version) + 1]) .where(APIResponse.id == context.current_parameters['id'])) Hope that helps, Simon On Fri, Nov 18, 2016 at 4:25 PM, Alexander O'Donovan-Jones <alexodonovanjo...@gmail.com> wrote: > That's a cool idea, but it would need to reference the instance we've > created to use the `id` atribute. > > 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. >> 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 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. -- 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.