mirsahib
3 months ago
357
3
Laravel

Illuminate/Database/QueryException while seeding the database with fake

Posted 3 months ago by mirsahib

I am getting the following error while seeding the database

Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tenant_id' in 'field list' (SQL: insert into `mother_meters` (`meter_number`, `assign_hrid`, `type`, `consume_unit`, `bill_amount`, `year`, `month`, `pay_status`, `tenant_id`, `updated_at`, `created_at`) values (05952960, 51751111, Sub Meter, 206, 2069, 1982, 05, Paid, 21, 2020-06-30 14:45:14, 2020-06-30 14:45:14))'

below is the model factory

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Model;
use Faker\Generator as Faker;


$factory->define(App\Tenant::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'nid' => $faker->ean13,
        'nid_img' => $faker->imageUrl($width = 640, $height = 480),
        'phone' => $faker->phoneNumber,
        'exp_rent' => $faker->numberBetween($min = 1000, $max = 3000),
        'paid_rent' => $faker->numberBetween($min = 1000, $max = 3000),
        'dues' => $faker->numberBetween($min = 1000, $max = 3000),
        'pay_date' => $faker->date($format = 'Y-m-d', $max = 'now'),
        'comment' =>$faker->text,
        'hrid' => $faker->ean8,
        'status' => $faker->boolean(1),
        'exit' => $faker->date($format = 'Y-m-d', $max = 'now'),

    ];
});

$factory->define(App\MotherMeter::class, function (Faker $faker) {
    return [
        //
        'meter_number'=>$faker->ean8,
        'assign_hrid'=>$faker->ean8,
        'type'=> $faker->randomElement(['Mother Meter','Sub Meter']),
        'consume_unit'=>$faker->numberBetween($min = 100, $max = 300),
        'bill_amount'=>$faker->numberBetween($min = 1000, $max = 3000),
        'year'=>$faker->year($max = 'now'),
        'month'=>$faker->month($max = 'now'),
        'pay_status'=>$faker->randomElement(['Paid','Not Paid'])

    ];
});
$factory->define(App\SubMeter::class, function (Faker $faker) {
    return [
        //
        'prev_reading'=>$faker->numberBetween($min = 1000, $max = 3000),
        'curr_reading'=>$faker->numberBetween($min = 3000, $max = 4000),
        'consume_unit'=>$faker->numberBetween($min = 100, $max = 300),
        'bill_amount'=>$faker->numberBetween($min = 2000, $max = 3000),
        'year'=>$faker->year($max = 'now'),
        'month'=>$faker->year($max = 'now'),
        'pay_status'=>$faker->randomElement(['Paid','Not Paid'])
    ];
});

Below is seeder factory class

<?php

use Illuminate\Database\Seeder;
use App\Tenant;
use App\MotherMeter;
use App\SubMeter;

class TenantSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        factory(Tenant::class, 20)->create()->each(function($tenant){

            $mother_meter = factory(MotherMeter::class)->make();
            $tenant->mother_meter()->save($mother_meter);
            $sub_meter = factory(SubMeter::class)->make();
            $mother_meter->sub_meters()->saveMany($mother_meter);
        });
    }
}

Here is the relationship diagram

https://imgur.com/RhR5enY

Please sign in or create an account to participate in this conversation.