Modelfactory is not generating $Faker data for some tables

Asked

Viewed 757 times

2

I’m using the ModelFactory standard of Laravel 5 some time ago, and everything was going ok, but from a few days to here stopped generating data from the library faker for some tables, although continue normally for others.

When I spin php artisan db:seed -vvv with the definitions below, the table client is filled correctly, with all values, and the table email ends with 100 lines of default or empty values.

Any idea where to start debugging this?

database/Factory/Modelfactory.php (default)

$factory->define(App\Client::class, function ($faker) {
    $origins = DB::table('origin')->lists('id');
    $methods = DB::table('entry_method')->lists('id');
    $image = str_replace('public', '', $faker->image('public/img', '100', '100', 'cats'));
    return [
        'name' => $faker->firstName,
        'middlename' => $faker->lastName,
        'lastname' => $faker->lastName,
        'birthday' => $faker->date(),
        'type' => $faker->randomElement(array('nao-associado','patrimonial','cooperador','coletivo','entidade','emerito','individual')),
        'associate_code' => $faker->randomNumber(),
        'identification' => str_random(20),
        'identification_type' => $faker->randomElement(array('rg','cpf','cnh','passaporte','social-security','outro')),
        'language' => $faker->randomElement(array('pt','en')),
        'foreigner' => $faker->boolean(),
        'image' => $image,
        'vip' => $faker->numberBetween(0,1),
        'id_origin' => $faker->randomElement($origins),
        'id_entry_method' => $faker->randomElement($methods),
    ];
});

$factory->define(App\Email::class, function ($faker) {
    $clients = DB::table('client')->lists('id');
    return [
        'email' => $faker->companyEmail,
        'type' => $faker->randomElement(array('pessoal','comercial')),
        'id_client' => $faker->randomElement($clients),
    ];
});

database/Seeds/Databaseseeder.php

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

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

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

        factory('App\Client', 100)->create();
        factory('App\Email', 100)->create();
        // create
        DB::statement('SET FOREIGN_KEY_CHECKS=1');

        Model::reguard();
    }
}

app/Email.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Email extends Model
{
    public $domains;
    public $timestamps = false;
    protected $table = 'email';
    protected $fillable = ['email', 'type', 'mailchimp_id', 'id_client'];

}

EDIT: Migrations

    Schema::create('client', function(Blueprint $table) 
    {
        $table->increments('id');
        $table->string('name', 50);
        $table->string('middlename', 50)->nullable();
        $table->string('lastname', 50);
        $table->date('birthday')->nullable();
        $table->enum('type', ['nao-associado','patrimonial',
                              'cooperador','coletivo',
                              'entidade','emerito',
                              'individual'])->default('nao-associado');
        $table->string('associate_code', 45)->nullable(); // codigo_socio
        $table->string('identification'); // documento identidade
        $table->enum('identification_type', ['rg','cpf','cnh','passaporte','social-security','outro']); // codigo_socio
        $table->enum('language', ['pt','en'])->default('pt'); 
        $table->boolean('foreigner')->default(false);
        $table->string('password', 255)->nullable(); // documento identidade
        $table->string('image', 255); // foto
        $table->boolean('vip')->default(false);

        // Foreign keys
        $table->integer('id_entry_method')->unsigned();
        $table->integer('id_origin')->unsigned();
        $table->integer('id_client')->unsigned()->nullable();
        $table->foreign('id_entry_method')->references('id')->on('entry_method');
        $table->foreign('id_origin')->references('id')->on('origin');
        $table->foreign('id_client')->references('id')->on('client');
        $table->timestamps();
        $table->softDeletes();
    });

    Schema::create('email', function(Blueprint $table) 
    {
        $table->increments('id');
        $table->string('email', 50);
        $table->enum('type', ['pessoal','comercial']);
        $table->string('mailchimp_id', 100);
        $table->integer('id_client')->unsigned();
        $table->foreign('id_client')->references('id')->on('client');
    });
  • Could you post the Migration of these two tables? I would like to play here. You may need to add a counter bar before running from the lists: \DB::

  • @gmsantos added Migrations. I haven’t tested yet with \DB:: but I find it unlikely that this is because the calls to DB work in client

1 answer

1


On your table email there is the field mailchimp_id which has not been defined in its Model Factory and also has no value default in Migration.

I modified your Factory here and it worked:

$factory->define(App\Email::class, function ($faker) {
    $clients = DB::table('client')->lists('id');
    return [
        'email' => $faker->companyEmail,
        'type' => $faker->randomElement(array('pessoal','comercial')),
        'mailchimp_id' => str_random(25),  
        'id_client' => $faker->randomElement($clients),
    ];
});

inserir a descrição da imagem aqui

  • bizarre, I did the same here and the error persists, including putting the field as nullable() in Migration. What amazes me is the db:seed not generate any error even with -vvv. I’m starting to get suspicious of timeouts or something.

  • @moraleida is using which DB? Here I checked with Mysql and just run db:seed verbose-free he started screaming PDO error.

  • Mysql here, running normal to other tables. very weird. has way to run only a specific Seed?

  • Can run a single file from Seed php artisan db:seed --class=UserTableSeeder

  • @moraleida what happens if you take FK out of Migration email?

  • nothing. rode composer update Just in case, I migrated the email to a class to run alone, I took the Fks, it’s all the same, blank email and defaults. +-----+-------+---------+--------------+
| id | email | type | mailchimp_id |
+-----+-------+---------+--------------+
| 1 | | pessoal | NULL |

  • Try to clear the email table before running Seed with DB::table('email')->truncate()

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.