Can’t create relationship using doctrine+CI

Do you have problem something like this when you try Codeigniter+Doctrine for the first time?

Fatal error: Uncaught exception ‘Doctrine_Export_Exception’ with message ‘SQLSTATE[HY000]: General error: 1005 Can’t create table ‘safarista-cms.#sql-160c_4d’ (errno: 150). Failing Query: “ALTER TABLE post ADD CONSTRAINT post_thread_id_thread_id FOREIGN KEY (thread_id) REFERENCES thread(id)”. Failing Query: ALTER TABLE post ADD CONSTRAINT post_thread_id_thread_id FOREIGN KEY (thread_id) REFERENCES thread(id)’ in C:\wamp\www\safarista-cms\application\plugins\doctrine\lib\Doctrine\Export.php:1219 Stack trace: #0 C:\wamp\www\safarista-cms\application\plugins\doctrine\lib\Doctrine\Export.php(1100): Doctrine_Export->exportClasses(Array) #1 C:\wamp\www\safarista-cms\application\plugins\doctrine\lib\Doctrine\Core.php(889): Doctrine_Export->exportSchema(NULL) #2 C:\wamp\www\safarista-cms\application\controllers\doctrine_tools.php(11): Doctrine_Core::createTablesFromModels() #3 C:\wamp\www\safarista-cms\system\codeigniter\CodeIgniter.php(236): Doctrine_Tools->create_tables() #4 C:\wamp\www\safarista-cms\index.php(115): require_onc in C:\wamp\www\safarista-cms\application\plugins\doctrine\lib\Doctrine\Export.php on line 1219

Then, you must be assign different structure for each field in two tables. If you follow some tutorial, maybe some of you used Doctrine::createTablesFromModels(); function. When you use that doctrine function, you should make sure to change the type of certain column.

When you assign one of your column/field  with “integer”, that function will interpret the integer into int, bigint, tinyint, etc according to the size. For example you want to set up one to many relationship like this

// ---------Model File user.php---------//
public function setTableDefinition() {
		$this->hasColumn('email', 'string', 255, array('unique' => 'true'));
		$this->hasColumn('password', 'string', 255);
	}
public function setUp() {
        $this->setTableName("category");
        $this->hasOne("User", array(
            "local" => "user_id",
            "foreign" => "id"
        ));
    }

// ---------Model File category.php---------//
public function setTableDefinition() {
        $this->hasColumn("category", "string", 32);
        $this->hasColumn("valid_status", "int", 1);
        $this->hasColumn("user_id", "int", 10);
    }
public function setUp() {
        $this->setTableName("category");
        $this->hasOne("User", array(
            "local" => "user_id",
            "foreign" => "id"
        ));
    }

When you use  Doctrine::createTablesFromModels();  both tables will have primary key “id” by the default. This “id” will have integer type and length by 10. I have not found the explanation anywhere on google world why column valid_status have type tinyint(4) and user_id have type bigint(20) by default. Which means, “id” (int, 10) in table user will have different structure with foreign key “user_id” (bigint, 10).

My simple solution  is manually changing the type of user_id in table category, lol.

What I know is that the transformation from integer to another type of int is caused by Doctrine default configuration. You can check that config in doctrine_pi.php in plugin folder. If you still wondering about the problem, there’re some people discuss this topic on this post‘s comments.

Hope this help😀

Satu pemikiran pada “Can’t create relationship using doctrine+CI

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s