 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
voda
Joined: 05 Oct 2009 Posts: 2
|
Posted: Tue Oct 06, 2009 1:30 am Post subject: Mdb2 - foreign keys |
|
|
Hello,
I am trying to create foreign keys in a mysql db. I am using the following code:
| 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 |
|
| Back to top |
|
 |
voda
Joined: 05 Oct 2009 Posts: 2
|
Posted: Fri Oct 09, 2009 10:45 pm Post subject: |
|
|
No one can help?
Maybe a working example would be enough. |
|
| Back to top |
|
 |
kingmaker
Joined: 08 Mar 2010 Posts: 2
|
|
| Back to top |
|
 |
Dschordschdabbelju
Joined: 03 Apr 2010 Posts: 1
|
Posted: Sat Apr 03, 2010 10:02 pm Post subject: Definitions constraint foreign key |
|
|
Hello,
I've got the same problem with the definitions array for foreign key contraint.
I searched the whole web, but found no solution.
Declaration has been contructed following http://78.129.214.25/manual/en/package.database.mdb2.intro-manager-module.php.
Code:
$table_options = array(
'comment' => 'books',
'charset' => 'latin1',
'collate' => 'latin1_german1_ci',
'type' => 'innodb',
);
$fields = array(
'lfdnr' => array(
'type' => 'integer',
'unsigned' => true,
'notnull' => 1,
// 'autoincrement' => true,
),
'reihenid' => array(
'type' => 'integer',
'unsigned' => true,
'notnull' => 1,
),
'text' => array(
'type' => 'text',
'length' => 3000,
),
);
$create = $mdb2->CreateTable('buecher', $fields, $table_options);
if (PEAR::isError($create)){
die ("(3):".$create->getMessage()."<br><br>".$create->getUserInfo());
}
$definition = array (
'primary' => true,
'fields' => array (
'lfdnr' => array(),
),
);
$constr = $mdb2->createConstraint('buecher', 'PRIMARY', $definition);
if (PEAR::isError($constr)){
die ("(4):".$constr->getMessage()."<br><br>".$constr->getUserInfo());
}
$definition = array (
'foreign' => true,
'fields' => array(
'reihenid' => array(),
),
'references' => array(
'table' => 'reihen',
'fields' => array(
'id' => array(),
),
),
);
$definition = array (
'primary' => false,
'unique' => false,
'foreign' => true,
'check' => false,
'fields' => array (
'reihenid' => array(
// 'sorting' => 'ascending',
'position' => 2,
),
),
'references' => array(
'table' => 'reihen',
'fields' => array(
'id' => array( //one entry per each referenced field
'position' => 1,
),
),
),
'deferrable' => false,
'initiallydeferred' => false,
'onupdate' => 'CASCADE',
'ondelete' => 'CASCADE',
'match' => 'SIMPLE',
);
$constr = $mdb2->createConstraint('buecher', 'reihen_ibfk_2', $definition);
if (PEAR::isError($constr)){
die ("( :".$constr->getMessage()."<br><br>".$constr->getUserInfo());
}
Error message:
( :MDB2 Error: insufficient data supplied
createConstraint: [Error message: invalid definition, could not create constraint] ... |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|