Laravel Error Message : SQLSTATE[42S22]: Column not found: 1054 Unknown column

Posted on

This article written to describe on how to resolve the error given or specified in the title as ‘Column not found: 1054 Unknown column’. It is connected with the web-app based application powered by Laravel framework. Below is the display of the error message :

QueryException in Connection.php line 761:
SQLSTATE[42S22]: Column not found: 1054 Unknown
column 'ip_internal' in 'field list' (SQL: insert into `server` (`type`, `sn`, `label`, `ip_internal`, `ip_public`, `ip_management`, `year`, `warranty`) values (x, x, x, x, x, x, x, x))

The error message is actually relates on the process of saving a record to a certain table and in the above error the name of that table is ‘server’. The error above is retrieved from a firebug additional plugin installed in Mozilla Firefox web browser.

In order to understand completely the problem, it is better to explain it in the following scenario. The scenario itself is described where there is a file which is a blade view template file and it is used to retrieve data or information from user. The element which is used to gather data or information from user in this file so that it is possible to be done is because it contains a form which is shown as follows :

{{ Form::open(array('url' => '/server/save_data', 'class' => 'form-horizontal', 'id' => 'home_form'))}}
...
{{ Form::close()}}

The form contains several input type field containing data The process of submitting the above form defined in the blade view template will be processed by the route definition which is defined in route file named web.php located in router folder in Laravel 5.3. It might also be located in App\Http\routes.php in any Laravel framework below the 5.3 version.  Furthermore, this is the router definition used in the url form target  which is defined in the route file :

Route::post('/server/save_data','ServerController@saveData');

So, the data is submitted from the form to be processed in a controller file named ‘ServerController’. It will be processed directly in the method named saveData. In this method, there is a process for saving the object which in this context it is the Server. It is shown below :

public function saveData(Request $request) {
        
        $server = new Server();
        $server->type = $request->input("type");
        $server->serial_number = $request["serial_number"];
        $server->label = $request["label"];
        $server->ip_internal = $request["ip_internal"];
        $server->ip_public = $request["ip_public"];
        $server->ip_management = $request["ip_management"];
        $server->label = $request["label"];
        $server->year = $request["year"];
        $server->warranty = $request["warranty"];

        $server->save();
        ...
}

In the above script the error is triggered in the line which is specified for saving the instance or object named ‘$server’. It is executing a query for saving a record where the data is represented by the previous line which is assigning one by one the column of the table by defining the property or attribute of the instantiated object $server. The error specified that there is no column named ‘ip_internal’.

After checking the table, calling save() method from the instantiated object $server is obviously will generate the error. It is because after checking one by one each field exist in the table, there is no column named ip_internal. Below is the description of the table named ‘server’ :

mysql> desc server;
Connection id:    3
Current database: laravel_db

+---------------+-------------+------+-----+---------+---------------+
| Field         | Type        | Null | Key | Default |Extra          |
+---------------+-------------+------+-----+---------+---------------+
| id_server     | int(5)      | NO   | PRI | NULL    |auto_increment |
...
| ip_int        | varchar(255)| YES  |     | NULL    |               |
...
+---------------+-------------+------+-----+---------+---------------+
... rows in set (0,71 sec)

mysql> 

As we retrieved the description of the table using the above executed command ‘desc table_name’. The name of the field or the column does not suitable with the one specified in the code. The field or the column name is ip_int but the attribute or the property which is called for the value assignment is ip_internal as specified in the following line of code :

$server->ip_internal = $request["ip_internal"];

Changing the field or column as specified in the above will solve the problem of saving the data or information through the process of calling method save() from a model inside the controller file within method saveData(). Below is the process of changing the column name :

mysql> alter table server change ip_int ip_internal varchar(255);
Query OK, 0 rows affected (0,07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>

After changing it to the suitable name for assigning attribute or property to an instantiated object of $server. Check the table again by redoing the following command :

mysql> desc server;
Connection id:    3
Current database: laravel_db

+---------------+-------------+------+-----+---------+---------------+
| Field         | Type        | Null | Key | Default |Extra          |
+---------------+-------------+------+-----+---------+---------------+
| id_server     | int(5)      | NO   | PRI | NULL    |auto_increment |
...
| ip_internal   | varchar(255)| YES  |     | NULL    |               |
...
+---------------+-------------+------+-----+---------+---------------+
... rows in set (0,71 sec)

mysql>

One thought on “Laravel Error Message : SQLSTATE[42S22]: Column not found: 1054 Unknown column

Leave a Reply