Error Lastval Is Not Yet Defined In This Session
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss currval of sequence is not yet defined in this session the workings and policies of this site About Us Learn more about postgres currval example Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow currval is not yet defined in this session Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each
Update Sequence Postgres
other. Join them; it only takes a minute: Sign up currval has not yet been defined this session, how to get multi-session sequences? up vote 4 down vote favorite 1 My objective is to get a primary key field automatically inserted when inserting new row in the table. How to get a sequence going from session to session in pg_get_serial_sequence PostgreSQL? doubleemploi@hanbei:/home/yves$ psql -d test Mot de passe : psql (8.4.13) Saisissez « help » pour l''aide. test=> create sequence test001 start 10; CREATE SEQUENCE test=> select currval('test001'); ERREUR: la valeur courante (currval) de la séquence « test00 » n''est pas encore définie dans cette session --- current value not yet defined this session (???) test=> select setval('test001', 10); setval -------- 10 (1 ligne) test=> select currval('test00'); currval --------- 10 (1 ligne) test=> \q test@hanbei:/home/yves$ psql -d test Mot de passe : psql (8.4.13) Saisissez « help » pour l''aide. test=> select currval('test001'); ERREUR: la valeur courante (currval) de la séquence « test00 » n''est pas encore définie dans cette session postgresql database-design auto-increment postgresql-8.4 share|improve this question edited Sep 19 '12 at 1:16 Erwin Brandstetter 218k29354465 asked Sep 18 '12 at 16:45 MUY Belgium 79011125 The whole purpose of a sequence is to have it session local. You can not "hand" the currval over to another session. Why do you think you need that? Why not do everything in
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about postgres setval hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges
Create Sequence In Postgresql
Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to find out if a sequence was initialized in this session? up vote 2 down vote favorite I need to read the current value of a sequence in a http://stackoverflow.com/questions/12481448/currval-has-not-yet-been-defined-this-session-how-to-get-multi-session-sequence function. However, for the first time in each session I try to use currval(), I get following error: currval of sequence "foo_seq" is not yet defined in this session Hint for those who might find this question by googling for this error: you need to initialize the sequence for each session, either by nextval() or setval(). I could use something like lastval() or even setval('your_table_id_seq', (SELECT MAX(id) FROM your_table)); instead, but this seems seems either prone to gaps http://stackoverflow.com/questions/24182521/how-to-find-out-if-a-sequence-was-initialized-in-this-session or slower than simple currval(). My aim is to avoid gaps and inconsistencies (I know some of the values will be added manually), so using nextval() before logic handling them is not ideal for my purpose. I would need this to initialize the sequence for the session anyway, but I would prefer to do something like this: --start of the function here IF is_not_initialized THEN SELECT setval('foo_seq', (SELECT MAX(id) FROM bar_table)) INTO _current; ELSE SELECT currval('foo_seq') INTO _current; END IF; --some magic with the _current variable and nextvalue() on the right position The point is that I have no idea how might "is_not_initialized" look like and whether is it possible at all. Is there any function or other trick to do it? EDIT: Actually, my plan is to let each group of customers choose between proper sequence, no sequence at all, and the strange "something like a sequence" I'm asking for now. Even if the customer wanted such a strange sequence, it would be used only for the columns where it is needed - usually because there are some analog data and we need to store their keys (usually almost gapless sequence) into the DB for backward compatibility. Anyway, you are right that this is hardly proper solution and that no sequence might be better than such a messy workaround in those situations, so I'll think (and discuss with customers) again wh
Sign in Pricing Blog Support Search GitHub This repository Watch 10 Star 68 Fork 27 xxgreg/dart_postgresql Code Issues 18 Pull requests 3 Projects 0 Pulse Graphs New issue Last insert ID problems #28 Closed kaisellgren opened this Issue Dec 17, 2013 https://github.com/xxgreg/dart_postgresql/issues/28 · 3 comments Projects None yet Labels None yet Milestone No milestone Assignees No one assigned 2 participants kaisellgren commented Dec 17, 2013 I noticed that calling query("SELECT LASTVAL()") right after an insert, does not always http://www.grocerycrud.com/forums/topic/308-gc-in-postgres-91/ work. Sometimes it does, sometimes it gives "lastval is not yet defined on this table". This means it's hard/impossible to retrieve ID's for content you inserted. I suspect it's because the then() callback for execute() is called before is not the information is actually saved. Am I correct? Owner xxgreg commented Dec 17, 2013 I don't think this is a driver problem. I'd recommend using a returning clause to obtain generated ids, as this means the insert and getting the id are done as one atomic operation. c.query('insert into my_table (id, value) values (default, 'blah') returning id;').single.then((row) { print(row.id); }); kaisellgren commented Dec 17, 2013 Ah-ha, with returning I had to use query rather than execute, otherwise is not yet I get the affected rows back. This will do. Thanks! Owner xxgreg commented Dec 17, 2013 When you get a chance. Could you post a question on stackoverflow about the lastval() error. I'd be interested to get an answer from a postgresql guru about what the database was actually doing. Also - google didn't find any results for "lastval is not yet defined on this table" - nor does it make any sense to me. Was the error message actually: "lastval is not yet defined in this session"? This would happen if nextval() hadn't been called yet on the connection before calling: 'select lastval()'; When using sequence functions - I'd recommend using currval('my_sequence'), instead of lastval(). With lastval() it's quite easy to accidentally get an id from the wrong sequence, which could lead to a hard to track down bug. Probably better to be explicit about which sequence you want to fetch the value from. xxgreg closed this Dec 17, 2013 hmmdeif referenced this issue Mar 6, 2014 Closed Feature: Insert with 'Returning' returns value rather than rows affected #29 Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment Contact GitHub API Training Shop Blog About © 2016 GitHub, Inc. Terms Privacy Security Status Help You can't perform that action at this time. You signed in with another tab or
CRUD website → grocery CRUD forum → Support → Bugs / Issues Javascript Disabled Detected You currently have javascript disabled. Several functions may not work. Please re-enable javascript to access full functionality. 3 GC in postgres 9.1 Started by jcasanova, Apr 13 2012 01:58 PM postgres install codeigniter Please log in to reply 15 replies to this topic #1 jcasanova jcasanova Advanced Member Members 59 posts Posted 13 April 2012 - 01:58 PM Hi all As GC doesn't support Postgres, I made some fixes/changes to the model and I got it working! TO DO: not too happy with one fix, about the insert_id, maybe someone could help. If anybody needs it just let me know. GREAT LIBRARY!!! Back to top #2 Nahum Nahum Newbie Members 1 posts Posted 16 April 2012 - 10:48 PM Hi, I would see your change. I start a new project with Codeigniter 2.1 and PostgreSQL 9.1. Back to top #3 jcasanova jcasanova Advanced Member Members 59 posts Posted 20 April 2012 - 11:56 AM Hi, let me packed up and upload it. I need to change some stuff of the new version released the day before yesterday. I'll keep you up Back to top #4 jcasanova jcasanova Advanced Member Members 59 posts Posted 26 April 2012 - 02:13 PM I merged the new version into the model that works with Postgres, maybe doesn't work a 100% , but forme work for almost everythingI attached the model and the library (library has a one little modification) , you could use the load_model function of GC, if you don't want to mess with the core.You have to do a one modification (I could not fixed from grocery model) to the postgre_driver.php of CI function insert_id() { $v = $this->_version(); $v = $v['server']; $table = func_num_args() > 0 ? func_get_arg(0) : NULL; $column = func_num_args() > 1 ? func_get_arg(1) : NULL; if ($table == NULL && $v >= '8.1') { $sql='SELECT LASTVAL() as ins_id'; } elseif ($table != NULL && $column != NULL && $v >= '8.0') { $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column); $query = $this->query($sql); $row = $query->row(); $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq); } elseif ($table != NULL) { // seq_name passed in table parameter $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table); } else { return pg_last_oid($this->result_id); } $query = $this->query($sql); $row = $query->row