user163831
user163831

Reputation: 1211

Error seeding table in Laravel?

I have a seeder that is the last one called in the DatabaseSeeder. When I call db:seed, I get the following error:

  [Illuminate\Database\QueryException]                                                        
  SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row  
  : a foreign key constraint fails (`catalara`.`models`, CONSTRAINT `models_manufacturer_id_  
  foreign` FOREIGN KEY (`manufacturer_id`) REFERENCES `manufacturers` (`id`)) (SQL: delete f  
  rom `manufacturers`)                                                                        

  [PDOException]                                                                              
  SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row  
  : a foreign key constraint fails (`catalara`.`models`, CONSTRAINT `models_manufacturer_id_  
  foreign` FOREIGN KEY (`manufacturer_id`) REFERENCES `manufacturers` (`id`))  

When I specify the class to seed, I just get this:

[Illuminate\Database\Eloquent\MassAssignmentException]  
  name  

Any idea as to how to resolve this?

See here for my Listing and Manufacturer model classes: How to set Eloquent relationship belongsTo THROUGH another model in Laravel?

Here is the seeder in question: use Faker\Factory as Faker;

class ListingsTableSeeder extends Seeder {

    public function run()
    {
        DB::table('listings')->delete();

        $faker = Faker::create();

        $modelCt = count(Model::all());
        $conditions = ['New', 'Used'];
        $layouts = ['Owner', 'Charter'];
        $hull_configs = ['Monohull', 'Catamaran', 'Trimaran'];

        foreach(range(1, 10) as $index)
        {

            Listing::create([
                'name' => ucwords($faker->firstName),
                'model_id' => rand(1,$modelCt),
                'length' => rand(1,160),
                'condition' => array_rand(array_flip($conditions)),
                'layout' => array_rand(array_flip($layouts)),
                'hull_config' => array_rand(array_flip($hull_configs)),
                'created_at' => new DateTime,
                'updated_at' => new DateTime
            ]);
        }
    }

}

I got it to work for some reason ONLY when running db:seed --class=ListingsTableSeeder but not when I run just db:seed even though it's the last seeder called in DatabaseSeeder. The same error comes up. But when I run it individually, it works, after I added protected $guarded = ['id'] to the Listing model for some reason.

But I'd like it to run with the rest of my seeders so why doesn't it and how can it please?

Upvotes: 1

Views: 2065

Answers (3)

Matt Burrow
Matt Burrow

Reputation: 11057

The foreign key is preventing you from seeding the database.

You can either drop it like Maximilian says or within your DatabaseSeeder where you register all your seeders to run put this surrounding all your Seeder classes.

DB::statement('SET FOREIGN_KEY_CHECKS=0;');

//... List of Seeder calls like
$this->call('some class');

DB::statement('SET FOREIGN_KEY_CHECKS=1;');

This will prevent all foreign keys preventing you from seeding your database.

Upvotes: 1

Maximilian Prepl
Maximilian Prepl

Reputation: 412

The problem here is your FOREIGN key you made earlier. models_manufacturer_id_foreign.

So you can't delete rows from table.

you need to drop it first.

and remake it when seed is completed.

Upvotes: 0

geoandri
geoandri

Reputation: 2428

I 'll start form the end

You ll have to add this in your Listing Class model

protected $fillable = array('name'); //or/and other fields that you want to exclude from mass assignement exception 

check this

For the first error i guess that you have set a foreign key (manufacturer_id) in the models table which references to the manufacture table (id column). So the error probably occurs in the models table seeder when you try to populate the table with a foreign key which does not exist (probably not yet?).

Upvotes: 0

Related Questions