Delete Non-Existent Record Causes Create?

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 12-Mar-2005 21:14:48   

I'm sure there is something I'm missing here, and there is a way around this, but for the moment I'm stumped.

I have to kind of pseudo code this, because it's a client database.

I have a "one to zero-to-one" relationship between two entities. The first entity is required for the second entity to exist, via an FK. I am trying to delete the first entity. As I do so, I need to ensure that I delete the dependent entity if it exists.

This is simplified--there are other factors involves (I must save recursively, save within a transactions, can't use cascading deletes, etc. etc.).

I tried this (remember, this is pseudocode):



Entity1.Entity2.Delete();
Entity1.Delete();


Note that the Entity2 table is currently completely empty.

When I save recursively, I get an INSERT failure on the Entity2 table, due to a non-nullable field.

So I tried this:



if (Entity1.Entity2.PK_id > 0)
{
Entity1.Entity2.Delete();
}
Entity1.Delete();


Same error. By checking a field of the entity, I am making a new entity?

Is there a way around this without creating Entity2 independently, fetching it using the Entity1 PK, testing to see if it exists, and then deleting it via Entity1? Doing it this way works, but it seems like there should be a better way.

I hope this question made sense.

Thanks in advance.

Phil

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 13-Mar-2005 01:08:26   

I can't spell "existent".

AND it posted my message twice, so I look even dumber than I really am.

simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 13-Mar-2005 12:06:06   

psandler wrote:

I can't spell "existent".

Done! wink

AND it posted my message twice, so I look even dumber than I really am.

simple_smile

Heh, I bookmarked the other thread. wink . I've removed it. I'll try to answer your questions today.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 13-Mar-2005 12:59:29   

psandler wrote:

I'm sure there is something I'm missing here, and there is a way around this, but for the moment I'm stumped.

I have to kind of pseudo code this, because it's a client database.

I have a "one to zero-to-one" relationship between two entities. The first entity is required for the second entity to exist, via an FK. I am trying to delete the first entity. As I do so, I need to ensure that I delete the dependent entity if it exists.

This is simplified--there are other factors involves (I must save recursively, save within a transactions, can't use cascading deletes, etc. etc.).

I tried this (remember, this is pseudocode):



Entity1.Entity2.Delete();
Entity1.Delete();

Note that the Entity2 table is currently completely empty.

When I save recursively, I get an INSERT failure on the Entity2 table, due to a non-nullable field.

Hmm.

It seems it does delete the entity from the db, but the recursive save doesn't skip the entities marked as deleted (In the save routine, a deleted entity isn't skipped). Because Entity2 isn't existing in the db, it creates a new one. You then call delete on it, that's not affecting any rows, and in the end you're saving entity1? (that's unclear to me, you show delete statements, but you speak about recursive saves).

Could you specify a bit more where the save comes into play?

Frans Bouma | Lead developer LLBLGen Pro
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 13-Mar-2005 15:13:49   

Otis wrote:

Hmm.

It seems it does delete the entity from the db, but the recursive save doesn't skip the entities marked as deleted (In the save routine, a deleted entity isn't skipped). Because Entity2 isn't existing in the db, it creates a new one. You then call delete on it, that's not affecting any rows, and in the end you're saving entity1? (that's unclear to me, you show delete statements, but you speak about recursive saves).

Could you specify a bit more where the save comes into play?

Sure. Entity1 is a collection that relates to a parent entity object. Let's call it Parent1.

So I am cycling through Parent1.colEntity1[x] and adding/deleting/updating the collection, in addition to updating the immediate attributes of Parent1.

During the routine, I need to add Entity2 if it needs to exist and doesn't, delete it if it needs to be deleted, and (in the case above) be sure I delete it if Parent1.colEntity1[x] is being deleted.

The routine also updates a second Entity collection. At the end of the routine, I call Parent1.Save(true);.

What I am doing is related to what we talked about in this thread:

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=2400

except in this case, the relationship table I am updating has an additional 1/0-1 relationship on it was well.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 14-Mar-2005 11:26:33   

Ok, in that case, you run into the issue I discussed briefly: after Delete() is called on an entity, and the delete succeeds, the entity's state is marked 'Deleted'. Then, the recursive save at some point arrives at the entity you called Delete() on and save it, if some fields are changed.

That's one possibility you can run into, though it's unlikely, as it would require you to change fields of an entity that's also deleted.

Another possibility is that you trigger lazy loading after you've deleted an entity. This then creates a new entity, as the entity isn't found in the db.

For example, if you do: entity1.Entity2.Delete(); and there isn't an Entity2 referenced nor in the database, it will first try to fetch Entity2, through lazy loading. This will give a new entity. Delete on that entity will fail, returning false. Saving entity1 will then create a new entity in the db. So if Delete() returned false, set entity1.Entity2 to null.

Frans Bouma | Lead developer LLBLGen Pro
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 14-Mar-2005 18:35:40   

Otis wrote:

So if Delete() returned false, set entity1.Entity2 to null.

Worked like a charm. Thanks again!