DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

When rails db:migrate error has occurred because I'd tried it several times.

😅 Mysql2::Error: Duplicate column name

Because the last db:migrate had been canceled, some columns had been created and not rollbacked.

root@ccf3d04c49ac:/app# bin/rails db:migrate
Caused by:
Mysql2::Error: Duplicate column name 'landlord_agent_type'
Enter fullscreen mode Exit fullscreen mode

🛠 Fix => Dive into MySQL and fix it by SQL

root@ccf3d04c49ac:/app# bin/rails db
Enter password:

MySQL [eshub_development]> show tables;
+-----------------------------+
| Tables_in_eshub_development |
+-----------------------------+
| contracts_rentals           |
+-----------------------------+
18 rows in set (0.004 sec)

MySQL [eshub_development]> desc contracts_rentals;
+-------------------------------+--------------+------+-----+---------+----------------+
| Field                         | Type         | Null | Key | Default | Extra          |
+-------------------------------+--------------+------+-----+---------+----------------+
| id                            | bigint       | NO   | PRI | NULL    | auto_increment |
| landlord_agent_type           | varchar(255) | YES  |     | NULL    |                |
| landlord_agent_id             | bigint       | YES  |     | NULL    |                |
+-------------------------------+--------------+------+-----+---------+----------------+
33 rows in set (0.005 sec)

MySQL [eshub_development]> alter table contracts_rentals drop column landlord_agent_type;
MySQL [eshub_development]> alter table contracts_rentals drop column landlord_agent_id;

MySQL [eshub_development]> show index from contracts_rentals;
+-------------------+------------+----------------------------------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table             | Non_unique | Key_name                                                 | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------------------+------------+----------------------------------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| contracts_rentals |          0 | PRIMARY                                                  |            1 | id            | A         |          17 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-------------------+------------+----------------------------------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
11 rows in set (0.004 sec)

MySQL [eshub_development]> exit
Bye
Enter fullscreen mode Exit fullscreen mode

Top comments (0)