Adding table names to error for duplicate column names

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-1, 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.1, 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.

Need help on your Ruby on Rails or React project?

Join Our Newsletter