Question About FK constraint and relation

livingwater

New member
Joined
Sep 3, 2017
Messages
1
Programming Experience
1-3
Hi, I'm a beginner in database programming and now I'm using VS2017 to create a database program.
I have 3 tables :
a. Article table, that contains ArtNo column and ArtName column where ArtNo is the primary key
b. Recipe table, contains RecNo column and RecName column where RecNo is the primary key
c. RecDetail, contains Id as the primary key, RecNo, ArtNo, and several columns

I created relationship between these tables using dataset designer. If I use "Relation Only" choice, there is no problem. But I want to select "Both Relation and Foreign Key constraint" because as far as I know, with this choice, If I want to delete a records on the parent table, then the records on the child table that contains the records from parent table will be deleted automatically too. But I'm encountering problem when inserting records on RecDetail table when I set the relation as "Both Relation and Foreign Key constraint"

Here's the step that I did :
1. Add records on article table, ex : A01, Corn and A02, Sugar
2. Add records on recipe table, ex : R01, Recipe01
3. Then I'm trying to add records on Recdetail table by using data bound combobox for RecNo and ArtNo (so, the RecNo and ArtNo will be filled with data that is already existed from the Recipe Table and Article Table), but then it generate exception errors : "Failed to enable constraint. One or more rows contain values that violating non-null, unique or foreign key constraints" and then on the datagrid control (I have datagrid control to display the Recdetail content) appear a red mark with this explanation : "ForeignKey Constraint FKArticle_RecDetail requires the child key values (A01) to exist in the parent table"

and this confuses me because the values (A01 from ArtNo column of Article Table) should already be existed in the parent table.

Can anybody help?

Thanks
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
If the error message says that you don't have that value present in the parent table then you don't. Obviously you think you do, but you mustn't. Maybe you're using two different DataSets or something like that.
 
Top Bottom