Has_one relationship #298
Labels
feature request
A feature that does not yet exist but will be a good addition to the library
medium
These issues are geared for people who have contributed to the project before
I believe that has_one is helpful and should be implemented. Although it behaves similarly to belongs_to, it has a semantic difference, and I find it more readable, and informative in well designed models and databases. It should indicate where the foreign key is placed. Generally, belongs_to should be used in the model that has the foreign key and has_one should be in the other.
I believe part of the confusion with the key order (when using has_one in the predecessor Orator ORM) is due to code that misuses has_one and belongs_to and therefore needs to specify both keys. When using the decorators with a well designed database that has
id
columns in tables and<other>_id
columns when reference other tables you would ideally not need to specify any keys or at worst the opposite key. This is why has_one has the other key first -- that is generally all you need to provide because the local key ofid
can be assumed (and hopefully omitted).That said, I'm not too stuck on the key order, although I do think it is logical. It is handy to allow specifying a single key in some cases. Perhaps it could use keyword arguments.
Here's an example:
I just reviewed my code to make sure I was using belongs_to and has_one consistently as I described in this issue and discovered that I wasn't in a few cases. Once I made corrections, I have relatively few usages of has_one. I should note that they are all using the
@has_one
decorator with no keys specified. Orator ORM made the correct assumptions about the keys. In order to migrate to Masonite ORM it would be a shame to have to use belongs_to losing the semantic meaning and needing to specify the keys.The text was updated successfully, but these errors were encountered: