

Adding table names to error for duplicate column names
source link: https://blog.saeloun.com/2023/04/18/add-table-names-to-error-for-duplicate-column-definitions
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Adding table names to error for duplicate column names
Apr 18, 2023
I'm a React and Rails enthusiast with two years of experience creating innovative software solutions that improve business productivity. I work well in teams and am always up for challenging projects. Born and raised in Navi Mumbai, I enjoy gaming and travelling in my free time.
One of the most underrated features of Rails is the way it handles proper error messages. It is very easy to understand the error and fix it. If we run a migration and it fails we get an actionable error message that tells us what went wrong and how to fix it. However, when we run a migration that adds a column to a table that already has a column with the same name, the error message that we get is not quite actionable since it does not tell us which table has a duplicate column. In the upcoming version of Rails 7, this issue will be fixed.
Before
Let’s create a table which includes two columns named same but of different types -
rails g migration create_testing_table testing_column, testing_column:integer
After we run rails db:migrate
for the above table, the following error is printed in the console -
StandardError: An error has occurred, this and all later migrations cancelled:
you can't define an already defined column 'testing_column'.
The above error indicates there cannot be another column named testing_column
because it already exists. This is one table so it’d be easier to debug, but suppose there are multiple migrations being run together and one of the migrations might have duplicate columns then it’d take some of our precious time to debug.
After
In the upcoming version of Rails 7, the error will tell us which table name the error comes from.
StandardError: An error has occurred, this and all later migrations cancelled:
you can't define an already defined column 'testing_column' on 'testing_tables'
Look at PR for more information.
Share this post!
Recommend
-
7
Removing rows in SQL that have a duplicate column value advertisements I have looked high and low on SO for an answer over the last couple of...
-
8
DELETE DUPLICATE VALUE IN THE SAME COLUMN - SQL #sql Aug 31 ...
-
4
Adding Another Column to a Table in my Web App » Stuart’s MATLAB Videos Here I am going to try and add an additional column to a table in my web app. Its been more than a year since I worked on this app, so I need to first f...
-
5
How to resol...
-
10
Count the number of duplicate values on a column for specific values from another column in MySQL? advertisements I've...
-
37
Know someone who can answer? Share a link to this Before answeringYou should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide m...
-
17
Gregor Wolf Oct 04 at 10:02 PM Analytical List Page fails with Error: adding element with duplicate id for OData V4 endpoint ...
-
7
Adding a Column to a Table in My Web App » Stuart’s MATLAB Videos Here I want to add a “Row Totals” column to a pivot table in the web app t...
-
4
Duplicate column names A former colleague from my MySQL AB days asked me aboutSELECT pk, SUM(amount) AS pk FROM t ORDER BY pk;should the duplicate use of the name PK be illegal? My reply...
-
13
How to Eliminate Duplicate Values Based on Only One Column of the Table in SQL? ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK