-
Notifications
You must be signed in to change notification settings - Fork 19
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
has_one does not always allow a unique constraint #35
Comments
This could potentially be addressed by getting the class of each has_one and checking: If that class has a |
Yep, totally, makes sense. I'm kind of wondering about similar use cases, but slightly different implementations too. When I've done this kind of thing in the past, I've used an instance method, rather than a That's the only concrete case I've got in mind right now, but my gut says that if there's this one use case that we've missed up until this point, there might be others as well that I'm not thinking of (which makes me just want to give users an escape hatch). |
That's a good point. An escape hatch is making more sense. |
@trptcolin @adsteel Bumping this old issue! We recently ran into this with two separate models that were both referencing a polymorphic field, one with For the escape hatch: are you thinking an additional option set on the class SomeModel
has_one :something, disable_consistency_fail_unique_constraint_check: true
# or
validates :something_else, uniqueness: true, disable_consistency_fail_unique_constraint_check: true
end |
I was thinking more like a config file passed to consistency_fail where you can say “ignore this association; I know about it already.” I’d rather not require users to decorate production code with knowledge of this specific tool. |
Hey @trptcolin is there a solution to this (if there's a config file it doesn't seem documented anywhere)? We have a very similar use case where a has_one association is a scoped version of a has_many association. Redefining the has_one association as an instance method is not appropriate for us because we join through this association a lot, e.g. most of the time we impose an ordering based on an attribute on the association. |
@louietyj hasn't been implemented yet, but I'd love a PR for it! |
A
has_one
may be a refinement of ahas_many
. In this case, there cannot and should not be a unique constraint on the foreign key. Currently the gem does not distinguish.The text was updated successfully, but these errors were encountered: