Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_last_id() causing an error and sequence question #8

Open
Aeon opened this issue Mar 28, 2017 · 1 comment
Open

get_last_id() causing an error and sequence question #8

Aeon opened this issue Mar 28, 2017 · 1 comment

Comments

@Aeon
Copy link
Member

Aeon commented Mar 28, 2017

On 1-May-2007, at 9:09am, brian wrote (Trac issue 1504):

This line was giving me an error.

return self.db().get_last_id(c,'ticket')

psycopg2.ProgrammingError: currval of sequence "ticket_id_seq" is not yet defined in this session

This seems utterly wrong to me since i can select currval('ticket_id_seq') via psql.
It's not used so i commented it out.

On a slightly different note, are the sequences in postgresql set correctly after all tickets, comments, stuff are inserted?

@Aeon Aeon self-assigned this Mar 28, 2017
@Aeon
Copy link
Member Author

Aeon commented Mar 28, 2017

On 16-Apr-2010, at 11:21am, scott wrote (Trac issue 1504#comment:1):

I ran into this as well.

The problem is that currval of a sequence id is a a session-local value. It cannot be retrieved unless nextval has been called for the current session.

I fixed this by modifying:
/usr/lib/python2.6/dist-packages/trac/db/postgres_backend.py

to call setval, max(id) first to update the sequence value for the session

+++ /usr/lib/python2.6/dist-packages/trac/db/postgres_backend.py
@@ -215,5 +215,6 @@
         return _like_escape_re.sub(r'/\1', text)
 
     def get_last_id(self, cursor, table, column='id'):
+ cursor.execute("SELECT SETVAL('%s_%s_seq', max(%s)) FROM %s" % (table, column,column,table))
         cursor.execute("SELECT CURRVAL('%s_%s_seq')" % (table, column))
         return cursor.fetchone()[0] 

@Aeon Aeon removed their assignment Mar 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant