mercredi 30 mars 2016

Integrity constraint violation using factory relationships in laravel 5.2

I am new to testing in laravel and I'm trying to create dummy data using Model Factories. I have three tables firms, clients, and briefs. A firm has many clients and a client has many briefs. The format of clients and briefs is very similar. They both have foreign keys firm_id and client_id respectfully. Whilst doing a phpunit test I want to create an instance of a client from a firm and an instance of a brief from the client using the following code:

$client = $firm->clients()
               ->save(factory(App\SearchFirmClient::class)->create());
$brief  = $client->briefs()
                 ->save(factory(App\SearchFirmBrief::class)->create());

$client is created without a fuss but $brief throws up an error:

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'client_id' cannot be null (SQL: insert into `search_firms_briefs` (`user_id`, `title`, `description`, `contact_id`, `client_id`, `updated_at`, `created_at`) values (1, Et in amet., Quo soluta ut impedit nesciunt autem. Laborum aperiam est non molestiae animi non quod. Explicabo eligendi doloribus ex quia vitae placeat ut., 0, , 2016-03-30 10:06:34, 2016-03-30 10:06:34))

The formats of both tables:

Schema::create('search_firms_clients', function(Blueprint $table)
{
    $table->increments('client_id');
    $table->integer('search_firm_id')->unsigned();
    $table->foreign('search_firm_id')->references('id')->on('search_firms')->onDelete('cascade');           
    $table->string('name');
    $table->softDeletes();
    $table->timestamps();
});

Schema::create('search_firms_briefs', function(Blueprint $table)
{
    $table->increments('brief_id');
    $table->integer('client_id')->unsigned();
    $table->foreign('client_id')->references('client_id')->on('search_firms_clients')->onDelete('cascade');         
    $table->string('title');
    $table->softDeletes();
    $table->timestamps();
});

The model factory for each:

$factory->define(App\SearchFirmClient::class, function ($faker) {
    return [
        'name'      => $faker->company,
        'email'     => $faker->companyEmail,
        'phone'     => $faker->phoneNumber,
        'address'   => $faker->address,
        'website'   => $faker->url,
    ];
});

$factory->define(App\SearchFirmBrief::class, function ($faker) {
    return [
        'user_id'       => 1,
        'title'         => $faker->sentence(3),
        'description'   => $faker->text(),
        'contact_id'    => 0,
    ];
});

The relationships:

class SearchFirm extends Model
{

    protected $table = 'search_firms';
    protected $primaryKey = 'id';

    public function clients() {
        return $this->hasMany('SearchFirmClient', 'search_firm_id');
    }
}

class SearchFirmClient extends Model
{

    use SoftDeletes;

    protected $table        = 'search_firms_clients';
    protected $primaryKey   = 'client_id';
    protected $dates        = [ 'deleted_at' ];


    public function briefs()
    {
        return $this->hasMany('SearchFirmBrief', 'client_id')->orderBy( 'updated_at', 'desc');
    }
}

class SearchFirmBrief extends Model
{

    use SoftDeletes;

    protected $table = 'search_firms_briefs';
    protected $primaryKey = 'brief_id';
    protected $touches = array('client');
    protected $dates = [ 'deleted_at'];

    public function client() {
        return $this->belongsTo('SearchFirmClient', 'client_id');
    }
}

Help me stackoverflow, you're my only hope.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire