Change id column name on Laravel 6.5.1

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Renaming Primary Keys in Laravel: Fixing the "Undefined Column" Error

As senior developers working with relational databases and frameworks like Laravel, we often encounter situations where the naming conventions between our application layer (Eloquent models) and the underlying database schema cause friction. The issue you are facing—where Eloquent expects an id column but your actual primary key is something else (cif_usu)—is a classic example of this mismatch.

This post will walk you through why this error occurs and provide the correct, robust way to handle custom primary keys in Laravel, ensuring your application remains clean, predictable, and performant.

The Problem: Mismatch Between Eloquent and the Database

You are attempting to query your User model using standard Eloquent conventions, which default to looking for an id column:

select * from "usuario" where "id" = 24 limit 1

However, based on your model definition:

public $primaryKey = 'cif_usu';

Laravel (and the underlying SQL queries) are looking for a column named id, but the actual primary key in your usuario table is named cif_usu. This results in the SQLSTATE[42703]: Undefined column: id error because the column simply does not exist under that name.

The fact that changing $primaryKey from public $primaryKey = 'cif_usu'; to protected $primaryKey = 'cif_usu'; did not resolve the issue confirms that the problem lies in how the database schema and the query logic interact, rather than just the visibility settings within the model class itself.

The Solution: Database Migrations are Key

The fundamental principle of database management is that the database is the source of truth. To fix this discrepancy permanently, you must ensure your database structure matches what Laravel expects, or vice versa. In almost all cases involving primary keys and table names in Laravel, the solution lies within your migrations.

Step 1: Correcting the Database Schema

If cif_usu is intended to be the unique identifier for the user (the Primary Key), you must ensure that any queries referencing this ID use the correct column name.

If you are using a custom key, it is best practice to let Eloquent know exactly which column to use when performing lookups. While you set $primaryKey in the model, the actual existence and naming of columns are enforced by your migration file.

Example Migration Fix:

When setting up your table, ensure that cif_usu is correctly defined as the primary key:

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

class CreateUsuariosTable extends Migration
{
    public function up()
    {
        Schema::create('usuario', function (Blueprint $table) {
            // Define the custom key as the primary key
            $table->string('cif_usu')->primary(); 
            
            // Other necessary columns...
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('usuario');
    }
}

By defining cif_usu as the primary key in the migration, you establish the correct structure for your database.

Step 2: Adjusting Eloquent Model Configuration

Once the database is correctly structured, you adjust your model to reflect that the key is not the default id. You should explicitly tell Eloquent what column represents the primary key and whether it is auto-incrementing.

In your User model (or User model if you are using a custom table name):

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable; // Assuming base class remains authenticatable

class User extends Authenticatable
{
    use Notifiable;

    // IMPORTANT: Specify the actual primary key column name
    protected $primaryKey = 'cif_usu'; 

    // Tell Eloquent that this field is not auto-incrementing (since it's a custom code)
    protected $incrementing = false; 

    // Define the table name if it deviates from the convention
    public $table = 'usuario'; 

    // ... other model properties
}

By setting $primaryKey to 'cif_usu' and $incrementing to false, you instruct Eloquent how to map between your PHP object and the database table. This ensures that when you call $user->id, it resolves to cif_usu, and more importantly, when Eloquent generates queries (like finding records), it uses cif_usu instead of attempting to find a non-existent generic id.

Conclusion

Dealing with custom primary keys in Laravel requires careful synchronization between your application code, your Eloquent model configuration, and your underlying database migrations. The error you encountered stems from the query expecting the default id, while the schema uses cif_usu. By mastering the use of migrations to define the correct structure and explicitly setting $primaryKey and $incrementing in your Eloquent model, you ensure that your application layer and database remain perfectly aligned. For deeper dives into structuring complex data relationships within Laravel, consulting resources like Laravel Company is highly recommended.