Header and Detail table check constraint

Posts   
 
    
tetris
User
Posts: 3
Joined: 05-Feb-2008
# Posted on: 05-Feb-2008 19:39:25   

Hello,

I have a header and detail tables. I'm populating data in the detail table, but I want to create a check constraint on it based on a combination of values in the header and detail table.

Basically, header table has a bit column called Tag_Required, if the bit is checked then I need to make sure I supply Tag information in the detail table, if Tag_Required is not checked, then I don't need to pass anything into that column.

Can LLBL do this, I looked into setting this up right on SQL, but Check constraints are limited only to the specific table I'm working in, I can't link to a header table, so I think.

Thanks in advance.

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 05-Feb-2008 20:22:29   

please see: Using the generated code - Validation per field or per entity

tetris
User
Posts: 3
Joined: 05-Feb-2008
# Posted on: 05-Feb-2008 20:40:28   

I can't seem to find that thread.. Do you have a link for it? Thanks.

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 05-Feb-2008 20:54:15   

It's not a thread, it is a section in the documentation for LLBLGen that is installed along with the product.

stefcl
User
Posts: 210
Joined: 23-Jun-2007
# Posted on: 06-Feb-2008 09:38:35   

Validation can help but could be tricky to set up because it involves a related entity which might be out of sync... or not fetched at all (especially if you're using adapter).

I suggest you create two distinct validator classes (one that requires a value for Tag and an another one that doesn't) and decide at runtime which one you attach to the detail entity.

You could create a method in your header entity class which returns an instance of the appropriate validator... Something like

public ValidatorBase GetDetailValidator()
{
     if(this.Tag_required)
          return new DetailValidatorWithTag();
     else
          return new DetailValidatorWithoutTag();
}

Or even easier, just one class which accepts a bool value in its constructor... It seems to be an elegant way to solve the problem... Anyway, I think I would use a database trigger to make sure it's impossible to insert records that violates the rule.