0

The full error:

System.Data.SqlClient.SqlException: the UPDATE statement conflicted with the FOREIGN KEY constraint \"FK__Product__Categor__1FCDBCEB\". The conflict occurred in database \"dbi416547_i416547\", table \"dbo.Category\", column 'CategoryID'.

Now I tried looking it up and found: Sql error on update : The UPDATE statement conflicted with the FOREIGN KEY constraint

But I don't know how to modify the foreign key in visual studio 2019. Also I don't understand where I would put the rule using T-SQL, because my table goes like:

CREATE TABLE [dbo].[Product] 
(
    [ProductID]   INT          IDENTITY (1, 1) NOT NULL,
    [UserID]      INT          NOT NULL,
    [Name]        NVARCHAR(50) NOT NULL,
    [Price]       DECIMAL(18)  NOT NULL,
    [Description] NVARCHAR(50) NOT NULL,
    [CategoryID]  INT          NOT NULL,
    [Photo]       NVARCHAR(50) NULL,
    [Likes]       INT          NULL,

    PRIMARY KEY CLUSTERED ([ProductID] ASC),

    FOREIGN KEY ([UserID]) REFERENCES [dbo].[User] ([UserID]),
    FOREIGN KEY ([CategoryID]) REFERENCES [dbo].[Category] ([CategoryID])
);

And if I put it underneath or between the ( ) I still get errors. Any suggestions?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
zhrgci
  • 584
  • 1
  • 6
  • 25
  • 4
    Look like at first you need to insert data into category table. – Basil Kosovan Dec 01 '19 at 11:47
  • @RohanRao Nope - adding the same row twice to Product would **not** cause this FK error. – SMor Dec 01 '19 at 15:01
  • 3
    The problem is not with the table and its definition. The problem lies within your code (presumably) which you do not show. You provided a value for CategoryID that does not exist in your Category table. – SMor Dec 01 '19 at 15:04
  • Yeah, the problem lied in the fact that CategoryID was converted from enums and since enums start with 0 it gave an error, thanks :p – zhrgci Dec 02 '19 at 07:53

0 Answers0