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

Created 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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.