
August 1st, 2009, 02:15 PM
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 1
Time spent in forums: 56 m 52 sec
Reputation Power: 0
|
|
|
Mdb2 - foreign keys
Hello,
I am trying to create foreign keys in a mysql db. I am using the following code:
PHP Code:
<?php
$dsn = 'mysql://user:pass@localhost/test?charset=utf8';
require_once 'MDB2.php';
$config = array('default_table_type' => 'INNODB' );
$mdb2 =& MDB2::singleton($dsn,$config);
if (PEAR::isError($mdb2))
die($mdb2->getMessage());
$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
$mdb2->loadModule('Manager');
$tableName = 'table1';
$definitions = array(
'id' => array(
'type' => 'integer',
'length' => 5,
'unsigned' => true,
'notnull' => true,
'autoincrement' => true,
),
'text' => array(
'type' => 'text',
'length' => 50,
'notnull' => true,
),
);
$mdb2->dropTable($tableName);
$state = $mdb2->createTable($tableName, $definitions);
if (PEAR::isError($state)){
die ("(1)".$state->getMessage());
}
$tableName = 'table2';
$definitions = array(
'id' => array(
'type' => 'integer',
'length' => 5,
'unsigned' => true,
'notnull' => true,
'autoincrement' => true,
),
'table1id' => array(
'type' => 'integer',
'length' => 5,
'unsigned' => true,
'notnull' => true,
),
'text' => array(
'type' => 'text',
'length' => 50,
'notnull' => true,
),
);
$mdb2->dropTable($tableName);
$state = $mdb2->createTable($tableName, $definitions);
if (PEAR::isError($state)){
die ("(2)".$state->getMessage());
}
$constraints = array(
'foreign' => true,
'fields' => array(
'table1id' => array(),
),
'references' => array(
'table' => 'table1',
'fields' => array(
'id' => array(),
),
),
'onupdate' => 'CASCADE',
'ondelete' => 'CASCADE', );
$state = $mdb2->createConstraint($tableName, 'FK', $constraints);
if (PEAR::isError($state)){
die ("(3)".$state->getMessage()."<br><br>".$state->getUserInfo());
}
?>
and this error:
(3)MDB2 Error: insufficient data supplied
createConstraint: [Error message: invalid definition, could not create constraint] [Last executed query: CREATE TABLE table2 (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, table1id BIGINT UNSIGNED NOT NULL, text VARCHAR(50) NOT NULL) ENGINE = INNODB] [Native code: 0]
So, what is wrong?
Thanks voda
|