SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'

Updated at 26-Jul-2021, By samar

The error SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id' occurs when you run the migration command using php artisan migrate if the column user_id (which is a foreign key) already exists in your table structure.

  • Check column exists or not in a table using SQL

    //SQL Syntax
    SHOW COLUMNS FROM `table` LIKE 'fieldname';
    
    //SQL Query
    SHOW COLUMNS FROM `posts` LIKE 'user_id';
    

    You can check if a column exists or not in MySQL using SQL query SHOW COLUMNS FROM `posts` LIKE 'user_id'; 

    This query will return the column name with the data type after executing the SQL query.

     

     

Back to code snippet queries related laravel