set next value of postgres sequence

1,646 views
Skip to first unread message

Chris Withers

unread,
Nov 27, 2015, 6:53:09 PM11/27/15
to sqlalchemy
Hi All,

What's the recommended way to set the next value of a postgres sequence
when using sqlalchemy?

cheers,

Chris

Jonathan Vanasco

unread,
Nov 28, 2015, 1:04:40 PM11/28/15
to sqlalchemy
There is a `Sequence` object that can be delcared like a table


seq = Sequence('some_sequence')
nextid = connection.execute(seq)

Chris Withers

unread,
Dec 1, 2015, 3:39:02 PM12/1/15
to sqlal...@googlegroups.com
Indeed, but that's not quite what I asked ;-)

I'm after setting the next value using, eg, setval:

http://www.postgresql.org/docs/current/static/functions-sequence.html

...or to do it on creation of the sequence:

http://www.postgresql.org/docs/current/static/sql-createsequence.html

I guess this might come into the stuff I was asking about in the "non-table DDL elements and MetaData objects" thread.

Chris
--
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+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

Jonathan Vanasco

unread,
Dec 1, 2015, 4:27:15 PM12/1/15
to sqlalchemy


On Tuesday, December 1, 2015 at 3:39:02 PM UTC-5, Chris Withers wrote:
Indeed, but that's not quite what I asked ;-)

I'm after setting the next value using, eg, setval:


Ah, sorry.  I saw "next value" and thought "nextval", not setval.  

The `Sequence` object just seems to support a 'next_value()' http://docs.sqlalchemy.org/en/latest/core/defaults.html#sqlalchemy.schema.Sequence

I looked in the source, and it looks like the postgres dialect only has a `nextval` operation.  Sqlalchemy's compiler and dialects seem to only support `create sequence` and `nextval` (there's no "alter sequence [restart with]" or "setval" equivalents).   It looks like you can only pass in a start value for the sequence on creation.

I think you may need to handle this with literal sql, and create a ticket or try to patch.  
Reply all
Reply to author
Forward
0 new messages