I am attempting to migrate 3 models using:
php artisan migrate
But I am given the error mentioned in the title This is the rest of the error message:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'created_at) null, `updated_at` timestamp(created_at) null, `created_at` times...' at line 1
This is my code:
Schema::create('v_users', function (Blueprint $table) { $table->id('userid'); $table->string('full_Name'); $table->string('email'); $table->string('phone_No'); $table->string('role'); $table->timestamps('created_at'); $table->timestamps('updated_at');
Schema::create('vehicles', function (Blueprint $table) { $table->id('v_id'); $table->foreign('user_id')->references('userid')->on('users')->onDelete('cascade'); $table->string('v_plate_no'); $table->string('v_brand'); $table->string('v_model'); $table->string('v_color'); $table->timestamps('created_at'); $table->timestamps('updated_at');
Schema::create('reports', function (Blueprint $table) { $table->id('reportid'); $table->foreign('vehicleid')->references('v_id')->on('vehicles')->onDelete('cascade'); $table->string('title'); $table->string('description'); $table->timestamps('created_at'); $table->timestamps('updated_at');
I am new to coding in general and would very much appreciate some feedback, thanks in advance.
Advertisement
Answer
The main thing that jumps out to me in your migrations is how you’re doing your timestamp columns.
The timestamps
method does not take a column name, see here:
https://laravel.com/docs/master/migrations#column-method-timestamps
The only argument for this method is precision, should you want to customize that. Right now you’re basically specifying a precision of created_at
which I can imagine would cause a query failure.
You just need to do this…
$table->timestamps();
… and Laravel will create both the created_at
and updated_at
columns for you.
If you need to explicitly create your own timestamp column, make sure you use the timestamp()
method (no ‘s’ at the end):
https://laravel.com/docs/master/migrations#column-method-timestamp