Re: serial values and odbc

From: Tibor Laszlo <ltibor(at)mail(dot)tiszanet(dot)hu>
To: pgsql-odbc(at)postgresql(dot)org
Cc: Alfonso Peniche <alfonso(at)iteso(dot)mx>
Subject: Re: serial values and odbc
Date: 2001-01-26 17:30:38
Message-ID: 01012618583901.13453@tir
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-odbc


> I am working with delphi through odbc to get to Postgres, but I have a
> problem:
>
> Everytime I try to make an insertion, I get a message from the odbc driver
> saying a Primary key value cannot be null (which is on purpose since I want
> postgres to use it serial value properties). Can anyone tell me if there's
> something special I have to do on the ODBC configuration, or how do I make an
> insertion through odbc?

I think it isn't ODBC related. But we use the following method:

Wth the DataSet's OnBeforPost method fetch the serial's NextValue and fill in
the Field in the DataSet like this:

QueryBeforPost(TDataSet* DaraSet)
{
if (DataSet->State == dsInsert &&
DataSet->FieldByName("<MySerialField">)->Value == Null)
{
TQuery* q = new TQuery(this);
q->DataBaseName = "<MyBDEDataBaseAliasName>";
q->SQL->Clear();
q->SQL->Add("SELECT nextval('<MyTable><MySerialField>_seq'");

// put these two statments into a try block...
q->Active = true;
DatSet->FieldByName("<MySerialField>")->Value =
q->FieldByName("nextval")->Value;

q->Active = false;
delete q;
}
}

Sorry, I'm a C++ programmer, so I can't write ObjectPascal code but I think it
will help.

This is the recommended method with PostgreSQL and with visual database
actions. You may put this code without the dsInsert condition into the
BeforInsert event to show the user the value but we prefer the one shown.

If the post fails - or the user cancels - the next nextval will send (the
next) safe value (see docs and archive). No matter the unused values. It works
even in a transaction block.

--
Tibor Laszlo
ltibor(at)mail(dot)tiszanet(dot)hu

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2001-01-26 17:47:42 Re: Hardwired MAXBACKENDS limit could be history
Previous Message Jan Wieck 2001-01-26 17:30:05 Re: Open 7.1 items

Browse pgsql-odbc by date

  From Date Subject
Next Message Joseph 2001-01-26 21:08:24 Rules and MS Access
Previous Message Dave Page 2001-01-26 12:16:32 Updated ODBC Driver Download