Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_index is unnecessary and may cause errors #1

Open
jaeming opened this issue Jan 31, 2017 · 1 comment
Open

add_index is unnecessary and may cause errors #1

jaeming opened this issue Jan 31, 2017 · 1 comment

Comments

@jaeming
Copy link

jaeming commented Jan 31, 2017

I was checking out the related tutorial for this and found an error in the migration:

add_index :users, :token

This doesn't make sense as indexing foreign keys can only happen between two different tables. You would not index a column to a table to which it already belongs. In other words, you'd only do something like this if Token was a separate table.

If you switch your db to postgres you'll get an error while trying to migrate:

PG::UndefinedTable: ERROR:  relation "users" does not exist
: CREATE  INDEX  "index_users_on_token" ON "users"  ("token")

The reason it work in SQLite is because SQLite basically ignores foreign keys.

@bparanj
Copy link
Owner

bparanj commented Feb 11, 2017

Good catch! I will update the article and the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants