lishaliu's avatar

seeding works in command line but not in phpunit test file

I run database seeding using php artisan db:seed everything works fine; but if I put $this->seed('DatabaseSeeder'); in the TestCase.php set up class after refresh database, I ran into issues: foreign key constraint....

this is my TestCase.php file:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication, RefreshDatabase;

    public function setUp()
    {
        parent::setUp();
        $this->seed('DatabaseSeeder');
    }
}

this is my DatabaseSeeder file:

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();
        Model::reguard();
        $this->call(AccountTableSeeder::class);
        $this->call(UserTableSeeder::class);
        $this->call(SampleTableSeeder::class);
        $this->call(AdminUsersTableSeeder::class);
        $this->call(RolesTableSeeder::class);
        $this->call(RoleUserTableSeeder::class);
        $this->call(ReportConfigTableSeeder::class);
        $this->call(ReportSnpInfoTableSeeder::class);
        $this->call(ReportSnpGenotypeTableSeeder::class);
        $this->call(QaCategoryTableSeeder::class);
        $this->call(QaQuestionaireTableSeeder::class);
        $this->call(QaQuestionaireFilterTableSeeder::class);
        $this->call(QaQuestionTableSeeder::class);
        $this->call(QaAnswerTableSeeder::class);
        $this->call(PromoCodesTableSeeder::class);
        $this->call(ProductCategoryTableSeeder::class);
        $this->call(ProductTableSeeder::class);
        $this->call(LocationsTableSeeder::class);
        $this->call(AgentListTableSeeder::class);
        $this->call(AdminUserProfilesTableSeeder::class);
    }
}

It works perfectly fine using command line; but errors as below if I run phpunit:

Caused by
Doctrine\DBAL\Driver\PDOException: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`mycodon`.`qa_question`, CONSTRAINT `qa_question_questionaire_id_foreign` FOREIGN KEY (`questionaire_id`) REFERENCES `qa_questionaire` (`questionaire_id`))

If I disable the foreign key constraint of every migration file, it works ofcourse.

I have no idea why this happen...

0 likes
3 replies
steve_laracasts's avatar
Level 7

Without seeing how you have defined your relations it's impossible to give a definitive answer, but this sounds like you have your foreign keys the wrong way round. i.e. in your table definition you're asking it to delete a parent before the child - delete the children first, then the parent and all will be good.

I don't know why this would work in the command line but not the seeder, presumably the integrity constraint checking is stronger in the latter. Either way, it doesn't matter, I think you need to fix your relations.

1 like
lishaliu's avatar

@KEL_ - Thank you so much, when you mentioned"delete", I realize that I have this line in my seeder file: \DB::table('qa_questionaire')->delete(); with foreign key set, I can not simply delete the database.

I deleted this line, and everything went well. But still why the command line work? Less restrict than the phpunit test?

Thank you very much

1 like
steve_laracasts's avatar

You're welcome, glad it worked out :)

I haven't looked at the code, so don't know, but I think so yes, it would explain what is happening.

Please or to participate in this conversation.