Link to home
Start Free TrialLog in
Avatar of PastorDwayne
PastorDwayne

asked on

SQL Junction Table

Good-day,

I am attempting to create a very simple database that uses a Junction Table. (see diagram)

Basically, what I would like to achieve is to assign one product to multiple categories;  When I run the code bellow, (in C#) I receive a constraint error;

I am wondering what  specifically is wrong with the diagram, and how I can change the DB to allow a product in multiple categories?

Thanks for your time
var adapterManager = new TableAdapterManager
            {
                ProductCategoryTableAdapter = new ProductCategoryTableAdapter(),
                CategoryTableAdapter = new CategoryTableAdapter(),
                ProductTableAdapter = new ProductTableAdapter()
            };
 
            adapterManager.UpdateAll(_dataSet);

Open in new window

help.gif
Avatar of madhuottapalam
madhuottapalam

If you are getting constraint error , you must be inserting child first and then Master table. Do master first and then child

Madhu
Avatar of Raja Jegan R
The INSERT order should be like Product, Category and ProductCategory or Category, Product and ProductCategory.

If you try to insert in ProductCategory before Product or Category you may receive a Constraint violation as its Product and Category ID's are not available in those tables.

Hope this helps
Avatar of PastorDwayne

ASKER

Thanks for your comments;

How would I change the following code to produce this result?

Thanks again;
var adapterManager = new TableAdapterManager
            {
                ProductCategoryTableAdapter = new ProductCategoryTableAdapter(),
                CategoryTableAdapter = new CategoryTableAdapter(),
                ProductTableAdapter = new ProductTableAdapter()
            };
 
            adapterManager.UpdateAll(_dataSet);

Open in new window

Also, the exact error I am getting is:

ConstraintException was unhandled
Column 'ProductID, CategoryID' is constrained to be unique.  Value '0, 2' is already present.
What are you trying to do is a Hierarchical update.
Try this one out.
var adapterManager = new TableAdapterManager
            {
                CategoryTableAdapter = new CategoryTableAdapter(),
                ProductTableAdapter = new ProductTableAdapter(),
                ProductCategoryTableAdapter = new ProductCategoryTableAdapter()
            };
 
            adapterManager.UpdateAll(_dataSet);

Open in new window

<< ConstraintException was unhandled
Column 'ProductID, CategoryID' is constrained to be unique.  Value '0, 2' is already present. >>

Then it means that you have values 0, 2 already in ProdcutCategory table and you are trying to update the same value 0, 2 for some other record.

Since you have ProductID and CategoryID combination as Primary Key or Unique Key combination you might obtain that error.
Thanks rrjegan17, I removed the two primary keys from the junction table, and ordered the update as you have mentioned;  however now I receive this error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ProductCategory_Product". The conflict occurred in database "MyDB", table "dbo.Product", column 'ProductID'.
The statement has been terminated.

Why would this be?  Also, is it alright for a SQL table to have no primary keys?

Thanks for your time
<< The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ProductCategory_Product". The conflict occurred in database "MyDB", table "dbo.Product", column 'ProductID'.
The statement has been terminated. >>

This means that you are trying to insert some records into ProductID column of ProductCategory table which is not present in the Product table. You have to first Insert some Products into Product table, some Categories into Category table and after that insert records into ProductCategory table to resolve this out.

<< I removed the two primary keys from the junction table >>

Does that mean deleted the records or removed the Primary keys itself. Have the Primary key constraint to ensure data integrity.
Thanks rrjegan17;

I re-added the Primary keys to the junction table, and there are no records in any of the tables.
 
No I receive this error

ConstraintException was unhandled
Column 'ProductID, CategoryID' is constrained to be unique.  Value '0, 2' is already present.

upon executing the following code:
_dataSet.ProductCategory.AddProductCategoryRow(_itemsRow, _categoriesRow);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, that's exactly it;  It was trying to add duplicate records,

Thanks rrjegan17 very much for your help!
Welcome