2 Replies - 376 Views - Last Post: 18 July 2011 - 09:30 AM Rate Topic: -----

#1 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Unable to link pk field to a sequence

Posted 15 July 2011 - 03:17 PM

the code below dynamically creates sequences and tables successfully but it's unable to link the pk field to a sequence when it's created. I tried the nextval('$seq') but it didn't work. I spent hours searching the web for a solution and I couldn't get no where. (im using postgresql) Help me please.

if($_REQUEST['fieldid'] == ''){
 if ($seq = $adb->pquery("CREATE SEQUENCE cust_".$columnName."_seq INCREMENT BY 1 MINVALUE 1000 MAXVALUE 999999999999999999"))
{
$log->debug("********seq: $seq created***");
} else{
$log->debug("********seq: $seq failed***");
}
$qur ="CREATE TABLE cust_".$columnName."(
".$columnName."id integer NOT NULL,
".$columnName." varchar(200) NOT NULL,
presence integer NOT NULL default '1',
picklist_valueid integer NOT NULL default '0',
PRIMARY KEY (".$columnName."id))";
$adb->pquery($qur, array());
}


This post has been edited by asdbabil: 15 July 2011 - 03:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to link pk field to a sequence

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

Re: Unable to link pk field to a sequence

Posted 16 July 2011 - 07:02 AM

Try

$newTable = "cust_$columnName";
$newSequence = "$newTable_seq";
if ($adb->pquery(
        "CREATE SEQUENCE $newSequence INCREMENT BY 1 MINVALUE 1000 MAXVALUE 999999999999999999"))
{
    $log->debug("********seq: $newSequence created***");
    $qur ="CREATE TABLE $newTable({$columnName}id integer NOT NULL DEFAULT nextval($newSequence), $columnName varchar(200) NOT NULL, presence integer NOT NULL default '1', picklist_valueid integer NOT NULL default '0', PRIMARY KEY ({$columnName}id))";
} else {
    $log->debug("********seq: $newSequence creation failed***");
}


or simply

$qur = "CREATE TABLE cust_{$columnName}({$columnName}id serial, $columnName varchar(200) NOT NULL, presence integer NOT NULL default '1', picklist_valueid integer NOT NULL default '0', PRIMARY KEY ({$columnName}id))";


because using the serial datatype buys you the sequence creation and attachment. For your reading pleasure.
Was This Post Helpful? 1
  • +
  • -

#3 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Unable to link pk field to a sequence

Posted 18 July 2011 - 09:30 AM

You have no idea how helpful that was!
Thank you :)

This post has been edited by asdbabil: 18 July 2011 - 04:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1