How to Solve Laravel Error Message : SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘column_name’ in ‘field list’

Posted on

This is an article where the focus is the error message triggered upon executing PHP Artisan Tinker to insert a new record into the table in the process of a development of a web-based application using Laravel as its main framework. The main error is shown in the following output :

user@hostname:/var/www/html/laravel$ php artisan tinker
Psy Shell v0.8.13 (PHP 7.0.24-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $category = new App\Category
=> App\Category {#734}
>>> $category->name = "Technical"
=> "Technical"
>>> $category->save()
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'field list' (SQL: insert into `categories` (`name`, `updated_at`, `created_at`) values (Technical, 2017-11-02 15:17:47, 2017-11-02 15:17:47))'
>>> quit
Exit: Goodbye.
user@hostname:/var/www/html/laravel$

The error shown in the above output is explaining that the column named ‘name’ doesn’t exist in the table which is utilized by the model named ‘Category’ so that the table named ‘categories’ which is associated with the model cannot be inserted. Check the table first as shown below :

user@hostname:/var/www/html/laravel$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 65
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use support_ticket;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+--------------------------+
| Tables_in_support_ticket |
+--------------------------+
| categories               |
| migrations               |
| password_resets          |
| tickets                  |
| users                    |
+--------------------------+
5 rows in set (0,00 sec)

mysql> select * from categories;
Empty set (0,00 sec)

mysql> desc categories;
+------------+-----------------+------+-----+---------+---------------+
| Field      | Type            | Null | Key | Default | Extra         |
+------------+-----------------+------+-----+---------+---------------+
| id         | int(10) unsigned| NO   | PRI | NULL    | auto_increment|
| created_at | timestamp       | YES  |     | NULL    |               |
| updated_at | timestamp       | YES  |     | NULL    |               |
+------------+-----------------+------+-----+---------+---------------+
3 rows in set (0,00 sec)

mysql> quit
Bye
user@hostname:/var/www/html/laravel$ cd app/

As shown in the above output, the columns of the table named ‘categories’ only consists of three columns named ‘id’, ‘created_at’, ‘updated_at’. So, there is no column named ‘name’. In order to be able to insert the record by setting the value of the attribute named ‘name’, the table must be changed so that it possess a column named ‘name’.

It can be done by doing it with two ways :

1. Changing the table with the query for altering the table. Just change it and add a new column named ‘name’. Below is the query to actually perform it :

ALTER TABLE categories ADD NAME VARCHAR(255) NOT NULL AFTER id

2. Using migration script file. It is located normally in the folder named ‘database/migrations/. Just rename the script which is responsible for modifying the table named ‘categories’ based on need which is adding a column named ‘name’. Below is the snippet code on the migration script file :

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}

The important part on the above snippet code is the  part of ‘$table->string(‘name’)’. It is adding a column named ‘name’ after the column named ‘id’.  After editing it, run the following command with the specific migration script file as the argument. To evade on executing all of the migration script files, move it into a specific folder so that the folder can be the actual parameter for the migration script file to be executed. The command is shown as follows :

php artisan migrate:refresh --path=/database/migrations/folder_where_the_migration_script_file_located

One thought on “How to Solve Laravel Error Message : SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘column_name’ in ‘field list’

Leave a Reply