Create PK with Trigger

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 15-Aug-2006 00:43:34   

Can anyone tell me if it will work to create the PK of a table using a trigger? What I'm thinking of doing is using a trigger to create a customer return RA#. I think this would be something similer to how I already create invoice numbers using a trigger. The difference in this case is that the value created will be the PK for the table. Will this work? What should I watch out for when using this as an LLBLGen entity?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 15-Aug-2006 02:47:22   

Not sure about anything other than you possibly not being able to refetch a new entity after a save. What is the type of the PK column? Why would you prefer to set the PK in the trigger?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 15-Aug-2006 11:14:03   

On Oracle, as the OracleDQE has logic to use a different kind of sequence retrieval code, you should set a config file parameter, see Application configuration through .config files in the documentation.

On SqlServer, set the sequence of the PK field to @@IDENTITY instead of SCOPE_IDENTITY(). Though as Brian asks: what's the type of the PK? It will only work with numeric sequences, not with a trigger which sets the PK to NEWID() or something.

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 15-Aug-2006 18:43:58   

The reason that I was thinking of setting the PK in the trigger is that the trigger was the only way I could think of auto generating the PK. I could use a stored precedure instead, but then I would still have to call the sp from the trigger to have this done automatically.

The column type i'm using is char (10) and I doing this in SQL Server 2000.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 16-Aug-2006 02:08:23   

The problem with this scenario is that if you create a new entity and then save it you cannot refetch it and will be out of synch with the database. Is there anyway that you could set the PK in code first and then save it to the database?

horo
User
Posts: 41
Joined: 01-Sep-2005
# Posted on: 17-Aug-2006 14:31:36   

Hi,

We have similar problem (on ORACLE)

Our trigger uses custom algorithm to generate PK. We can not Save and refetch this entity with adapter model.

Any ideas?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 17-Aug-2006 15:02:47   

A workaround is to create a new field maybe an Identity field, and then use the designer to set this field as the PK, rather than the database PK (trigger generated).