Generated unique constraints for foreign keys

Posts   
 
    
sne
User
Posts: 10
Joined: 08-Oct-2012
# Posted on: 08-Oct-2012 15:49:56   

Hello all,

the documentation states that:

"It's not necessary to define a unique constraint for foreign key fields participating in a 1:1 relationship: LLBLGen Pro creates these unique constraints in the Relational Model Data automatically for you. Any unique constraint defined over the same fields is therefore redundant."

My question is: is there a way to disable the generation of these unique constraints (in the SQL scripts and mapping files) within the LLBLGen designer?

Thank you, Stefan

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 08-Oct-2012 17:12:41   

No, as it would make it possible to insert the same FK value twice, which makes it no longer a 1:1 relationship. You can of course model the relationship as 'model-only' (so it doesn't have a backing FK+UC)

Could you elaborate a bit why you'd need this? I.e.: why you'd need a 1:1 relationship while there's no UC on the FK?

Frans Bouma | Lead developer LLBLGen Pro
sne
User
Posts: 10
Joined: 08-Oct-2012
# Posted on: 09-Oct-2012 09:51:57   

Thanks for the quick reply.

It's actually a 1:0..1 (UserAccount:Language) relationship, where the foreign key field has to be nullable (in case there is no matching entity of type Language for a particular entity of type UserAccount).

I'm getting a unique constraint violation error while inserting two UserAccounts that have no matching Language:

"Cannot insert duplicate key in object 'dbo.UserAccount'. The duplicate key value is (<NULL>)."

I've tried making the relationship Model-only but then the foreign key constraint isn't generated either (makes sense).

I'm using NHibernate and MS SQL Server.

Thanks again.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-Oct-2012 19:35:47   

I've tried making the relationship Model-only but then the foreign key constraint isn't generated either (makes sense).

That's the only way as I see it. You mark the relation as model-only. Then you define the FK in the database. Subsequent UPDATE scripts shouldn't remove the FK, if I'm not missing anything.

sne
User
Posts: 10
Joined: 08-Oct-2012
# Posted on: 10-Oct-2012 10:44:53   

Thanks for clarifying it, much appreciated.

Cheers, Stefan