Testingunit not recognize foreign keys

Hi everyone,

I’m trying to use UnitTesting with yii in order to test some insertions in my comment table.

So the first step i did is to create the CommentTest.php class with the following code:

 $comment=new Comment;


	        'content'=>'comment 1',






When i execute the test i get the following error message:

1) MessageTest::testApprove

...1452 Cannot add or update a child row: a foreign

key constraint fails (`blogt`.`comment`, CONSTRAINT `FK_comment_user` FOREI

GN KEY (`author_id`) REFERENCES `user` (`id`))


Tests: 1, Assertions: 0, Errors: 1.

But the author_id 3 already exist in my user table. To be sure i have no problem with key constraints, i execute this sql code in a console:

INSERT INTO Comment (content, author_id) VALUES("my first comment",3) 

and the insertion succeded with no error.

So the problem is with the yii UnitTesting, and i dont know why and what exactly.

Thanks for helping :)

Actually it should give you an error if you are trying to insert a record that is already there.

Actualy, no error should be displayed, because the author_id 3 already exist in the user table. The error should be displayed if the author_id that i want to insert in the Comment table doesn’t exist in the user table because the author_id is a foreign key of the id in the user table. I’m wrong ?

Hmm. You are right. Looks like you don’t have a corresponding record in user table. Are you sure you’ve initialized your DB data properly before running a test?

Yes i’m 110 % sure, otherwise i will have an error message telling me [color="#FF0000"]CDbException: CDbConnection failed to open the DB connection[/color]. I thing the problem comes from the yii UnitTesting which doesn’t

recognize very well the foreign keys or something like that. Weird but i still investigate

Samdark : Did you tried Testingunit with yii trying to insert a foreign key value ?

Yes. I have several applications tested without having such a problem.

[quote="samdark, post:7, topic:30770"]

Yes. I have several applications tested without having such a problem.


Ok,do you an example(ClassTest.php) showing how you insert a data using a value of a foreign key ? cause i don't know why i have this problem.

This is the data from the User table:

[code]id  username

1   user1

2   user2

3   user3

4   user4

Data from the Comment table:

id  content     author_id

1   comment1    2

2   comment2    4 

3   comment3    1

4   comment4    1

As you can see, the author_id 3 that i want to insert in the comment table already exist in the user table and i dont understand why i have a foreign key contraint error. I tried to insert the same comment using the sql console, and it works. That’s why

i think its a problem from the Testingunit.

Here’s my code again:

public function testApprove()


 $comment=new Comment;


                'content'=>'comment 1',







that i miss something ? The test.php in my config looks like this:

return CMap::mergeArray(








                                'connectionString' => 'mysql:host=localhost;dbname=db-tests',

                                'emulatePrepare' => true,

                                'username' => 'root',

                                'password' => '',

                                'charset' => 'utf8',

                        		'tablePrefix' => '',





and the connection to my DB is working cause i inserted some datas in an other table without using a foreign key value


Can you post SQL dump for db-tests DB so I’ll try to reproduce it?

I know where is the problem now. In the beforesave method i have:


And when i insert a comment

in a console using the test unit, the user->id is set to 0.

Thanks anyways :)