Ecto has_one association doesn't have clear vision for multiple results case

178 views
Skip to first unread message

Artur Cygan

unread,
Mar 16, 2017, 1:19:59 PM3/16/17
to elixir-ecto
Hey! 

I'm checking `has_one` association and seems that if there are more results than one, then just first one is silently chosen. This situation is ambiguous. Shouldn't it behave as `Repo.one` - raising an error? Such constraint should be anyway enforced in good database design with unique constraint. However sometimes we forget about this and it would be good to know when database gets out of expected consistency so we know what to fix.

Regards,
Artur

José Valim

unread,
Mar 16, 2017, 2:05:59 PM3/16/17
to elixi...@googlegroups.com
I believe the idea is that you could have associations that would use an order_by to select one. For example, most_voted_comment that would have an order_by of VOTES DESC. This has never be implemented afaik.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/e30fed9c-dae0-4355-a96d-893af171573f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michał Muskała

unread,
Mar 17, 2017, 7:49:19 AM3/17/17
to elixi...@googlegroups.com
Maybe it would be helpful to, at least, have a warning in that case? This seems like it might be a source of indeterministic bugs (since the databases generally don't guarantee the order of results, unless an order clause is specified). Or maybe an option to has_one to be more conservative and enforce this?

Right now we don't specify in the docs what will happen in such a case, so squinting enough we could go either way keeping the backwards compatibility (saying before it was an "undefined behaviour").

Michał.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4LTHh0dnh1T4R1v3Gq-Ynu6KknWVtnzfTNsSQphvV9g4g%40mail.gmail.com.

José Valim

unread,
Mar 17, 2017, 7:53:38 AM3/17/17
to elixi...@googlegroups.com
If we make it break, then we should provide an option that brings the current behaviour back.



José Valim
Skype: jv.ptec
Founder and Director of R&D

On Fri, Mar 17, 2017 at 12:47 PM, Michał Muskała <mic...@muskala.eu> wrote:
Maybe it would be helpful to, at least, have a warning in that case? This seems like it might be a source of indeterministic bugs (since the databases generally don't guarantee the order of results, unless an order clause is specified). Or maybe an option to has_one to be more conservative and enforce this?

Right now we don't specify in the docs what will happen in such a case, so squinting enough we could go either way keeping the backwards compatibility (saying before it was an "undefined behaviour").

Michał.

On 16 Mar 2017, 19:05 +0100, José Valim <jose....@plataformatec.com.br>, wrote:
I believe the idea is that you could have associations that would use an order_by to select one. For example, most_voted_comment that would have an order_by of VOTES DESC. This has never be implemented afaik.



José Valim
Skype: jv.ptec
Founder and Director of R&D

On Thu, Mar 16, 2017 at 6:19 PM, Artur Cygan <arczi...@gmail.com> wrote:
Hey! 

I'm checking `has_one` association and seems that if there are more results than one, then just first one is silently chosen. This situation is ambiguous. Shouldn't it behave as `Repo.one` - raising an error? Such constraint should be anyway enforced in good database design with unique constraint. However sometimes we forget about this and it would be good to know when database gets out of expected consistency so we know what to fix.

Regards,
Artur
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/e30fed9c-dae0-4355-a96d-893af171573f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.

Artur Cygan

unread,
Mar 17, 2017, 8:57:03 AM3/17/17
to elixir-ecto, jose....@plataformatec.com.br
I believe that default behavior should enforce uniqueness, to bring back current way there could be "unique: false" option to the association. For example:

has_one :most_voted_comment, App.Comment, unique: false

And by default `unique: true`. When `unique: true` and multiple records are returned we can raise and include tip to create unique constraint for this case in the database or to pass `unique: false`. What do you think?

W dniu piątek, 17 marca 2017 12:53:38 UTC+1 użytkownik José Valim napisał:
If we make it break, then we should provide an option that brings the current behaviour back.



José Valim
Skype: jv.ptec
Founder and Director of R&D

On Fri, Mar 17, 2017 at 12:47 PM, Michał Muskała <mic...@muskala.eu> wrote:
Maybe it would be helpful to, at least, have a warning in that case? This seems like it might be a source of indeterministic bugs (since the databases generally don't guarantee the order of results, unless an order clause is specified). Or maybe an option to has_one to be more conservative and enforce this?

Right now we don't specify in the docs what will happen in such a case, so squinting enough we could go either way keeping the backwards compatibility (saying before it was an "undefined behaviour").

Michał.

On 16 Mar 2017, 19:05 +0100, José Valim <jose....@plataformatec.com.br>, wrote:
I believe the idea is that you could have associations that would use an order_by to select one. For example, most_voted_comment that would have an order_by of VOTES DESC. This has never be implemented afaik.



José Valim
Skype: jv.ptec
Founder and Director of R&D

On Thu, Mar 16, 2017 at 6:19 PM, Artur Cygan <arczi...@gmail.com> wrote:
Hey! 

I'm checking `has_one` association and seems that if there are more results than one, then just first one is silently chosen. This situation is ambiguous. Shouldn't it behave as `Repo.one` - raising an error? Such constraint should be anyway enforced in good database design with unique constraint. However sometimes we forget about this and it would be good to know when database gets out of expected consistency so we know what to fix.

Regards,
Artur
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages