Can change Primary Key field, but not by using relation

Posts   
 
    
Posts: 34
Joined: 03-Oct-2005
# Posted on: 23-Nov-2005 11:09:07   

Hi

I have an entity with a compound primary key (2 fields). One of these fields is also the foreign key in a (LLBGEN) relation with another entity.

In code I can change the value of the field directly - this fires the entity contents changed event


Product.ProductTypeID = 32

however if I assign a new related entity to the relation field the related entity is changed but the event is not fired and the field value is not changes


ProductTypeEntity.ID = 32
DA.FetchEntity(ProductTypeEntity)
Product.ProductTypeEntity = ProductTypeEntity 'no event

This leads to a situation where my product has a ProductTypeID of one value, and a related ProductTypeEnity with a different ID ...!

I found a couple of previous threads about changing the value of a primary key field - to be consistent shouldn't it be possible to change the related entity as well...?

TIA

Matt

Paul.Lewis
User
Posts: 147
Joined: 22-Aug-2005
# Posted on: 24-Nov-2005 04:08:57   

It's never good practice to change primary keys be they singleton or compound. Please consider creating a new entity with the new composite key values and assigning the related entity to it. When you save, set the recusive flag to true.

Changing the primary key will result in referential integrity issues that you don't want.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 24-Nov-2005 10:14:18   

MattTrinder wrote:

Hi

I have an entity with a compound primary key (2 fields). One of these fields is also the foreign key in a (LLBGEN) relation with another entity.

In code I can change the value of the field directly - this fires the entity contents changed event


Product.ProductTypeID = 32

however if I assign a new related entity to the relation field the related entity is changed but the event is not fired and the field value is not changes


ProductTypeEntity.ID = 32
DA.FetchEntity(ProductTypeEntity)
Product.ProductTypeEntity = ProductTypeEntity 'no event

This leads to a situation where my product has a ProductTypeID of one value, and a related ProductTypeEnity with a different ID ...!

The field isn't changed because it's a PK field. As Paul said, it will lead to (could lead to) referential integrity issues. Effectively: changing the PK is the same as adding a new entity and removing the old.

Frans Bouma | Lead developer LLBLGen Pro