Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a Table called 'GridGroups' with a primary field 'GridGroupID'

 

There is a secondary table called 'GridTrans' which also has a field, 'GridGroupID', which is not primary.

 

This second table uses a ForeignKey relationship to the primary for the two GridGroupID's. Don't ask me why - not my database, and I don't like Foreign Keys for just this reason.

 

Now I want to delete the GridGroups, so (and correct me if I am wrong) I should:

 

1. Delete all of the rows in GridTrans with this GridGroupID

2. THEN delete the one row in GridGroups with that GridGroupID

 

I can do (1), (2) throws this error:

 

Integrity constraint violation: Primary key for row in table 'GridGroups' is referenced by foreign key...

 

Have I missed a step here? This, by the way, is a Sybase database

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

Posted

If you can/may change the database and the FK, then you should set the FK delete rule to cascade.

Cascade means that when you want to delete/update the row in primary table then the rows are updated/deleted from the tables with foreign key too. In your case you should then delete only from table GridGroups and matched rows from GridTrans would deleted automaticly.

 

Other option, if you can't change the FK rule...

You don't say in your error with which table the integrity constraint violation appears? I mean there may be other tables with FK's to GridGroups table. If so, then you can't delete from GridGroups unless you first delete all matching rows from all other tables with relations.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...