Framework: [ModelFactory] Referencing migration without $table->timestamps() while running create()/make() causes undefined SQL error

Created on 2 Feb 2017  路  3Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.8
  • PHP Version: 7.1.1
  • Database Driver & Version: "PostgreSQL 9.5.5 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 6.2.1 20160916 (Red Hat 6.2.1-2), 64-bit"

Description:

Running the factory using _factory(App\User::class)->create()_ resulted invalid SQL with following error:

Illuminate\Database\QueryException with message 'SQLSTATE[42703]: Undefined column: 7 ERROR:  column "updated_at" of relation "employees" does not exist
LINE 1: ... "gender", "position_id", "employment_status_id", "updated_a...
                                                             ^ (SQL: insert into "employees" ("first_name", "middle_name", "last_name", "gender", "position_id", "employment_status_id", "updated_at", "created_at") values (Reid, Howell, Weissnat, male, 3, 2, 2017-02-02 14:24:22, 2017-02-02 14:24:22) returning "employee_id")'

SHOULD NOT CAUSE ERROR SINCE I INTEND TO EXCLUDE _$table->timestamps()_ (updated_at and created_at columns).

Steps To Reproduce:

  1. Update the migration table for user and add the following:
Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('employee_id')->unique();
            $table->string('username')->unique();
            $table->string('password');
            $table->rememberToken();
        });
  1. Create a migration schema with the following: php artisan make:model -m Employee
  2. Update the migration table for employee and add the following:
Schema::create('employees', function (Blueprint $table) {
            $table->increments('employee_id');
            $table->string('first_name');
            $table->string('middle_name');
            $table->string('last_name');
            $table->enum('gender', ['male', 'female']);
            $table->date('date_employed')->default('now()');
            $table->integer('position_id')->default(1);
            $table->integer('employment_status_id')->default(1);
            $table->unique(['first_name', 'middle_name', 'last_name'], 'name');
        });
  1. Update the ModelFactory.php file and add the following:
$factory->define(App\User::class, function (Faker\Generator $faker) {

    return [
        'username'          => $faker->userName,
        'employee_id'       => function(){
            return \factory(\App\Employee::class)->create()->employee_id;
        },
        'password'          => bcrypt('secret'),
        'remember_token'    => str_random(10),
    ];
});

$factory->define(App\Employee::class, function (\Faker\Generator $faker) {

    $genders = ['male', 'female'];
    $gender = $genders[\random_int(0, 1)];

    return [
        'first_name'            => ('male' === $gender) ? $faker->firstNameMale : $faker->firstNameFemale,
        'middle_name'           => $faker->lastName,
        'last_name'             => $faker->lastName,
        'gender'                => $gender,
        'position_id'           => \random_int(1, \App\Position::all()->count()),
        'employment_status_id'  => \random_int(1, 3)
    ];
});
  1. Open tinker to test the factory: php artisan tinker
  2. Execute the following command: factory(App\User::class)->create();
  3. Following error will occur:
Illuminate\Database\QueryException with message 'SQLSTATE[42703]: Undefined column: 7 ERROR:  column "updated_at" of relation "employees" does not exist
LINE 1: ... "gender", "position_id", "employment_status_id", "updated_a...
                                                             ^ (SQL: insert into "employees" ("first_name", "middle_name", "last_name", "gender", "position_id", "employment_status_id", "updated_at", "created_at") values (Reid, Howell, Weissnat, male, 3, 2, 2017-02-02 14:24:22, 2017-02-02 14:24:22) returning "employee_id")'

Most helpful comment

@gabbydgab The reason you are seeing the error is because you haven't disabled timestamps for App\Employee & App\User model. You need to disable these in your models like this:

public $timestamps = false;

All 3 comments

@gabbydgab The reason you are seeing the error is because you haven't disabled timestamps for App\Employee & App\User model. You need to disable these in your models like this:

public $timestamps = false;

What is timestamp here.. why we are dealing with!
Any honest !

Cheer!
By default, Eloquent will maintain the created_at and updated_at columns on your database table automatically. Simply add these timestamp columns to your table and Eloquent will take care of the rest. If you do not wish for Eloquent to maintain these columns, add the following property to your model:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

progmars picture progmars  路  3Comments

kerbylav picture kerbylav  路  3Comments

JamborJan picture JamborJan  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments