iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤮

How I Wasted 4 Hours on a Typo in a Rails Migration

に公開2件

I don't think this will be useful to anyone, but I'm writing this as a self-reminder.

The Problem

A query to add a foreign key constraint like this works fine:

ALTER TABLE `hoge` ADD CONSTRAINT fk_hoge FOREIGN KEY (`foo`) REFERENCES `fuga` (`bar`);

But a migration like this:

add_foreign_key :hoge, :fuga, colmun: :foo , primary_key: :bar, name: :fk_hoge

was rejected with the error Key column 'fuga_id' doesn't exist in table, and I struggled with it for 4 hours.

The Cause

Wrong:

add_foreign_key :hoge, :fuga, colmun: :foo , primary_key: :bar, name: :fk_hoge

Right:

add_foreign_key :hoge, :fuga, column: :foo , primary_key: :bar, name: :fk_hoge

Did you notice it?

......

Wrong:

colmun

Right:

column

I even went as far as tracing the ActiveRecord source code before I noticed this...

Since it was saying Key column 'fuga_id' doesn't exist in table, I should have realized that it was looking for the default column name because the part responsible for specifying the table column for the foreign key wasn't working correctly...

How does everyone fight against typos in dynamic languages like this? Is it through linters like RuboCop?

References

I didn't even need to trace it this far...

Discussion

catnosecatnose

これは気づかないですねー笑
linterで検出してくれない部分については「気をつける」以外なさそうな…?

1
北山淳也北山淳也

@catnose さん
コメントありがとうございます。
そうなんです😂 何度も何度も確認してヨシ!となったはずなのですが結局typoでした……