Page 1 of 1

How to use Sequence to generate primary key

Posted: Wed 05 May 2010 18:43
by Nieuwsma
I'm using dotConnect's LINQ. Our postgres db uses sequences to generate unique primary integer keys. How do I get the nextval from a sequence? I cannot use ExecuteCommand since it doesn't return the new value. Please provide an example.
Thanks for your help.

Posted: Thu 06 May 2010 15:19
by AndreyR
Here is a simple way:
1. Create a trigger in DB that will be populating the autoincrement field.
2. Set Auto Generated Value to true for the autoincrement PK column.
3. Set the AutoSync property to the OnInsert.
This will do the trick - the PK values will be populated after inserting.

Anyway without trigger?

Posted: Thu 06 May 2010 15:31
by Nieuwsma
Thanks for the response, AndreyR.

We have other code that uses ODBC to work with the database. It would all need to be changed to use the trigger. Is there a way to use sequences without changing the database?

Posted: Fri 07 May 2010 11:49
by AndreyR
Try defining the column in the following way:

Code: Select all

id integer DEFAULT nextval('autoinc_seq'::regclass)
Set the properties like I have described in the previous post, this should help.
Please let me know if anything goes wrong.