inputs for pg_get_id() function?
От | Steve Lefevre |
---|---|
Тема | inputs for pg_get_id() function? |
Дата | |
Msg-id | 466F59DE.3070608@osu.edu обсуждение исходный текст |
Ответы |
Re: inputs for pg_get_id() function?
|
Список | pgsql-novice |
Hello all -- I'm migrating from the MySQL world to Postgres. I'm used to having the mysql_insert_id function to get the id of a row I just inserted. Several people on the list turned me on to using nextval or currval to get an id *before* I do an insert, to avoid race conditions . Now I'm working on a PHP function that I'm calling pg_get_id(). When I first wrote it, my inputs were $table_name and $column_name, but I quickly found out that I have to reference the name of the columns' sequence, not the column itself, to get the id. Now it's working. But, I'm wondering it I can even get rid of the $sequence_name input. If I assume that a user will only call the function to get an number for a primary key column that is a sequence, is there a query I can do to find out the primary key of a table, and the name of it's sequence? In psuedo-code: function pg_get_id ( $table ) { "SELECT primary_key_column_name FROM $table" "SELECT column_sequence_name FROM $table " "SELECT nextval('column_sequence_name') FROM $table" return $nextval; }
В списке pgsql-novice по дате отправления: