Unique constraints on 1 to 1 links
(Created page with "When you have a 1 to 1 relation between classes a small (e) appears on the link that is embedded (where the key is actually stored). Technically you can store the key in both...")
 
(Automatically adding template at the end of the page.)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
When you have a 1 to 1 relation between classes a small (e) appears on the link that is embedded (where the key is actually stored).
When you have a 1-to-1 relation between classes, a small (e) appears on the embedded link (where the key is stored).


Technically you can store the key in both ends but that is sort of just asking for trouble and nothing good comes out of it. So the recommendation is to have only one (e ) in the association.
Technically, you can store the key on both ends, but that is sort of asking for trouble - nothing good comes of it. The recommendation is to have only one (e) in the association.


A single link is implemented in the exact same way as a multilink – i.e a foreign key on one end. The fact that it is a single link is handled further up in the model logic.
A single link is implemented in the same way as a multilink – i.e a foreign key on one end. The fact that it is a single link is handled further up in the model logic.


This has apparently left a bug that I have not been able to track down as of yet – a single link that points to two or more objects.
This has left a bug I have been unable to track down yet – a single link pointing to two or more objects.


When I found this fact in a running system I started to think about how to stop that from happening. And the obvious way is add a unique constraint to the foreign key when it is a 1-1 link. But a straight up unique constraint will disallow multiple nulls and that is not what we want or need – since null is on if the association is 0..1-1.
When I found this fact in a running system, I started to think about how to stop that from happening. The obvious way is to add a unique constraint to the foreign key when it is a 1-1 link, but a straight-up unique constraint will disallow multiple nulls which is not what we want or need – since null is on if the association is 0..1-1.


Then I found that at least SQLServer supports indexes with criterias like this:
I found that at least SQLServer supports indexes with criteria like this:
CREATE unique  NONCLUSTERED INDEX IX_test ON ConsolidationRequest


CREATE unique  NONCLUSTERED INDEX IX_test ON ConsolidationRequest
(WinnerID) where  WinnerID  is not null
Dailybuilds onwards does this – but since not all DB’s like it, there is a flag in the DBConfig; SupportsFilteredIndex.


(WinnerID) where  WinnerID  is not null
BoringPredictableServer sets this to true for MSSql, but not for SQLCe of MySql.
 
[[Category:OCL]]
Dailybuilds onwards does this – but since not all DB’s like it there is a flag in the DBConfig; SupportsFilteredIndex.
{{Edited|July|12|2024}}
 
BoringPredictableServer sets this to true for MSSql, but not for SQLCe of MySql…

Latest revision as of 15:49, 10 February 2024

When you have a 1-to-1 relation between classes, a small (e) appears on the embedded link (where the key is stored).

Technically, you can store the key on both ends, but that is sort of asking for trouble - nothing good comes of it. The recommendation is to have only one (e) in the association.

A single link is implemented in the same way as a multilink – i.e a foreign key on one end. The fact that it is a single link is handled further up in the model logic.

This has left a bug I have been unable to track down yet – a single link pointing to two or more objects.

When I found this fact in a running system, I started to think about how to stop that from happening. The obvious way is to add a unique constraint to the foreign key when it is a 1-1 link, but a straight-up unique constraint will disallow multiple nulls which is not what we want or need – since null is on if the association is 0..1-1.

I found that at least SQLServer supports indexes with criteria like this:

CREATE unique  NONCLUSTERED INDEX IX_test ON ConsolidationRequest
(WinnerID) where  WinnerID  is not null

Dailybuilds onwards does this – but since not all DB’s like it, there is a flag in the DBConfig; SupportsFilteredIndex.

BoringPredictableServer sets this to true for MSSql, but not for SQLCe of MySql.

This page was edited 92 days ago on 02/10/2024. What links here