PostgreSQL next value of the sequences?

后端 未结 7 1841
天命终不由人
天命终不由人 2020-12-01 14:20

I am using PostgreSQL for my Codeigniter website. I am using grocery crud for add, edit and delete operations. While doing an edit or add, I want to rename an uploaded file

7条回答
  •  情歌与酒
    2020-12-01 14:32

    The previously obtained value of a sequence is accessed with the currval() function.

    But that will only return a value if nextval() has been called before that.

    There is absolutely no way of "peeking" at the next value of a sequence without actually obtaining it.

    But your question is unclear. If you call nextval() before doing the insert, you can use that value in the insert. Or even better, use currval() in your insert statement:

    select nextval('my_sequence') ...
    
    ... do some stuff with the obtained value
    
    insert into my_table(id, filename)
    values (currval('my_sequence'), 'some_valid_filename');
    

提交回复
热议问题