简体   繁体   English

我正在使用db forge进行数据库迁移。add_foreign_key将密钥从第一个创建的表添加到第二个表

[英]I am using db forge for database migration.add_foreign_key add keys from first created table to second one

I am using db forge for database migration in ci.When I add foreign key in first created table. 当我在第一个创建的表中添加外键时,我正在使用db forge进行ci中的数据库迁移。 It works but in second table foreign key from first table inherit so it create problem to create the sencond table. 它可以工作,但是在第二个表中,第一个表的外键继承,因此创建第二个表会产生问题。 Here is my code 这是我的代码

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Initial_schema {
    function up() 
    {
        $CI =& get_instance();
        if($CI->migrate->verbose)
            echo "Creating tables...";
        // create table TblRowStatusEnum
        if(! $CI->db->table_exists('TblRowStatusEnum')) {
            $cols = array(
                'id' => array('type' => 'INT', 'constraint' => 11, 'auto_increment' => TRUE,'null'=>FALSE),
                'status' => array('type' => 'enum("Active","Inactive","Delete")', 'null' => FALSE),
                'description' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => True),
                'createdAt' => array('type' => 'datetime', 'null' => FALSE),
                'updatedAt' => array('type' => 'datetime', 'null' => TRUE)

            );

            // Setup Keys
            $CI->dbforge->add_key('id', TRUE);
            $CI->dbforge->add_field($cols);
            $CI->dbforge->create_table('TblRowStatusEnum', TRUE);

            //insert data
            /*$insert = array('status' => 'Active','description' =>'Active');
            $CI->db->insert('TblRowStatusEnum', $insert);
            $insert = array('status' => 'Inactive','description' =>'Inactive');
            $CI->db->insert('TblRowStatusEnum', $insert);
            $insert = array('status' => 'Delete','description' =>'Delete');
            $CI->db->insert('TblRowStatusEnum', $insert);*/

    }

    // create table TblUser
    if(! $CI->db->table_exists('TblUser')) {
        $cols = array(
        'id' => array('type' => 'INT', 'constraint' => 11,'auto_increment' => TRUE,'null'=>FALSE),
        'facebookId' => array('type' => 'INT','constraint' => '11', 'null' => FALSE),
        'firstName' => array('type' => 'VARCHAR','constraint' => '255', 'null' => FALSE),
        'middleName' => array('type' => 'VARCHAR','constraint' => '255', 'null' => TRUE),
        'lastName' => array('type' => 'VARCHAR','constraint' => '255', 'null' => FALSE),
        'email' => array('type' => 'VARCHAR','constraint' => '255', 'null' => FALSE),
        'address' => array('type' => 'TEXT'),
        'dateOfBirth' => array('type' => 'DATE'),
        'facebookAuthToken' => array('type' => 'TEXT', 'null' => FALSE ,'comment'=>"For storing auth token for offline access. If we don''t need offline access this column can be deleted"),
        'profileImageUrl' => array('type' => 'TEXT','null' => FALSE),
        'rowStatus' => array('type' => 'INT','constraint' => '11','null' => FALSE),
        'createdAt' => array('type' => 'DATETIME', 'null' => FALSE),
        'updatedAt' => array('type' => 'DATETIME', 'null' => TRUE)
        );

        // Setup Keys
        $CI->dbforge->add_key('id', TRUE);
        $CI->dbforge->add_key('rowStatus');
        $CI->dbforge->add_field($cols);




        //gives CONSTRAINT FOREIGN KEY `fk_testTable_testField` (`myField`)
        //      REFERENCES testTable(`testField`) ON DELETE CASCADE ON UPDATE CASCADE

        $CI->dbforge->add_foreign_key(array('field' => 'rowStatus',
        'foreign_table' => 'TblRowStatusEnum',
        'foreign_field' => 'id'));
        $CI->dbforge->create_table('TblUser', TRUE);
    }

    //TblSellingItem
    if(! $CI->db->table_exists('TblSellingItem')) {
        $cols = array(
        'id' => array('type' => 'INT', 'constraint' => 11,'auto_increment' => TRUE,'null'=>FALSE),
        'title' => array('type' => 'VARCHAR','constraint' => '255', 'null' => FALSE),
        'description' => array('type' => 'TEXT'),
        'price' => array('type' => 'FLOAT','null' => FALSE),
        'postedBy' => array('type' => 'INT','constraint' => 11,'null' => FALSE),
        'rowStatus' => array('type' => 'INT','constraint' => 11,'null' => FALSE),
        'createdAt' => array('type' => 'datetime', 'null' => FALSE,'default'=>get_now()),
        'updatedAt' => array('type' => 'datetime', 'null' => TRUE)

        );

        // Setup Keys
        $CI->dbforge->add_key('id', TRUE);
        $CI->dbforge->add_key('postedBy');
        $CI->dbforge->add_key('rowStatus');
        $CI->dbforge->add_field($cols);
        $CI->dbforge->add_foreign_key(array('field' => 'rowStatus',
           'foreign_table' => 'TblRowStatusEnum',
           'foreign_field' => 'id'));
       $CI->dbforge->add_foreign_key(array('field' => 'postedBy',
       'foreign_table' => 'TblUser',
       'foreign_field' => 'id'));
        $CI->dbforge->create_table('TblSellingItem', TRUE);


    }

    //TblItemImage
    if(! $CI->db->table_exists('TblItemImage')) {
        $cols = array(
        'id' => array('type' => 'INT', 'constraint' => 11, 'auto_increment' => TRUE,'null'=>FALSE),
        'sellingItemId' => array('type' => 'INT','constraint' => '11', 'null' => FALSE),
        'url' => array('type' => 'TEXT','null' => FALSE),
        'imageOrder' => array('type' => 'INT','null' => FALSE),
        'rowStatus' => array('type' => 'INT','constraint' => 11,'null' => FALSE),
        'createdAt' => array('type' => 'datetime', 'null' => FALSE,'default'=>get_now()),
        'updatedAt' => array('type' => 'datetime', 'null' => TRUE)

        );

        // Setup Keys
        $status =array('field' => 'rowStatus',
        'foreign_table' => 'TblRowStatusEnum',
        'foreign_field' => 'id');
        $posted_by =array('field' => 'sellingItemId',
        'foreign_table' => 'TblSellingItem',
        'foreign_field' => 'id');
        $CI->dbforge->add_key('id', TRUE);
        $CI->dbforge->add_key('sellingItemId');
        $CI->dbforge->add_key('rowStatus');
        $CI->dbforge->add_field($cols);
        $CI->dbforge->add_foreign_key($status);
        $CI->dbforge->add_foreign_key($posted_by);
        $CI->dbforge->create_table('TblItemImage', TRUE);



    }


    //TblChatMessage
    if(! $CI->db->table_exists('TblChatMessage')) {

    $cols = array(
    'id' => array('type' => 'INT', 'constraint' => 11, 'auto_increment' => TRUE,'null'=>FALSE),
    'sellingItemId' => array('type' => 'INT','constraint' => '11', 'null' => FALSE),
    'message' => array('type' => 'TEXT'),
    'sender' => array('type' => 'INT','null' => FALSE),
    'receiver' => array('type' => 'INT','null' => FALSE),
    'sentTime' => array('type' => 'datetime', 'null' => FALSE),
    'messageStatus' => array('type' => 'INT','constraint' => 11,'null' => FALSE),
    'rowStatus' => array('type' => 'INT','constraint' => 11,'null' => FALSE),
    'createdAt' => array('type' => 'datetime', 'null' => FALSE,'default'=>get_now()),
    'updatedAt' => array('type' => 'datetime', 'null' => TRUE)

    );

    // Setup Keys
    $CI->dbforge->add_key('id', TRUE);
    $CI->dbforge->add_key('sellingItemId');
    $CI->dbforge->add_key('sender');
    $CI->dbforge->add_key('receiver');
    $CI->dbforge->add_key('rowStatus');
    $CI->dbforge->add_field($cols);

    $CI->dbforge->add_foreign_key(array('field' => 'rowStatus',
    'foreign_table' => 'TblRowStatusEnum',
    'foreign_field' => 'id'));
    $CI->dbforge->add_foreign_key(array('field' => 'sellingItemId',
    'foreign_table' => 'TblSellingItem',
    'foreign_field' => 'id'));
    $CI->dbforge->add_foreign_key(array('field' => 'receiver',
    'foreign_table' => 'TblUser',
    'foreign_field' => 'id'));
    $CI->dbforge->add_foreign_key(array('field' => 'sender',
    'foreign_table' => 'TblUser',
    'foreign_field' => 'id'));

    $CI->dbforge->create_table('TblChatMessage', TRUE);





    }


    }

    function down() 
    {
        $CI =& get_instance();
        if($CI->migrate->verbose)
        echo "Dropping table accounts...";
        $CI->dbforge->drop_table('TblRowStatusEnum');
    }
}

?>

Error is like this 错误是这样的

A Database Error Occurred

Error Number: 1072

Key column 'postedBy' doesn't exist in table

CREATE TABLE IF NOT EXISTS `TblItemImage` ( `id` INT(11) NOT NULL AUTO_INCREMENT, 
`sellingItemId` INT(11) NOT NULL, `url` TEXT NOT NULL, `imageOrder` INT NOT NULL, 
`rowStatus` INT(11) NOT NULL, `createdAt` datetime DEFAULT '2014-03-19 11:51:13' NOT NULL, 
`updatedAt` datetime NULL, PRIMARY KEY `id` (`id`), FOREIGN KEY `rowStatus` (`rowStatus`) 
REFERENCES `TblRowStatusEnum`(`id`), FOREIGN KEY `rowStatus` (`rowStatus`) REFERENCES 
`TblRowStatusEnum`(`id`), FOREIGN KEY `postedBy` (`postedBy`) REFERENCES `TblUser`(`id`), 
FOREIGN KEY `rowStatus` (`rowStatus`) REFERENCES `TblRowStatusEnum`(`id`), FOREIGN KEY 
`sellingItemId` (`sellingItemId`) REFERENCES `TblSellingItem`(`id`), KEY `sellingItemId` 
(`sellingItemId`), KEY `rowStatus` (`rowStatus`) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Filename: C:\xampp\htdocs\plunder_migration\system\database\DB_driver.php

Line Number: 330

here FOREIGN KEY postedBy is add automatically in table TblItemImage. 此处,FOREIGN KEY postedBy将自动添加到表TblItemImage中。 what is wrong with my code? 我的代码有什么问题? Thank You. 谢谢。

您可以通过以下代码更改发布的约束来进行测试,并将结果提供给我们吗?

postedBy' => array('type' => 'INT','constraint' => 11,'null' => TRUE),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM