Search code examples
rubysqlitesequel

sequel one_to_one relation between two tables fails


I have two tables with one_to_one relation between them

Table one: User - has user related columns Table two: Security_Option - has security related columns and user_id (which references the User table)

In user.rb I have

one_to_one :security_option

Similarly in security_option.rb I have

one_to_one :user

When I have a User object accessing the security_option works but when I have SecurityOption object and try to access the user I get a sql exception

Sequel::DatabaseError: SQLite3::SQLException: no such column: users.security_option_id

I understand that I can add a security_option_id column in users and that will solve the problem but shouldn't it be possible to get the user by the user_id in the security table?


Solution

  • one_to_one should be used on one side of the association, and many_to_one should be used on the other side. The model containing the foreign key should use many_to_one, the model being referenced by the foreign key should use one_to_one. See http://sequel.jeremyevans.net/rdoc/files/doc/association_basics_rdoc.html.