How not to break this primary key when synchronizing with Azure?

Project Summary

We are creating a “digital” kitchen where you can add what items are in your refrigerator, freezer, etc. You can then view these items in a web application so that you know the products that you have in the kitchen on the go.

Database

Only three tables.

Lists

It contains identifier lists and the names of these lists, for example, "Refrigerator" or "Freezer". So this is basically a "container".

Items

Then there are Elements that contain types of items, that is, Milk, 1 gallon.

ListItems

ListItems ( ), . - , , ListItems - . , , , , "", " ". - , .

ListItems , . . , Item Unique, - , , "Ham", "". ""

. . - 3 , 200 , 28 . - , , 500 31 : enter image description here

ListID 1 , . ItemID 1 , Ham.

? .

, Azure :

{"Violation of PRIMARY KEY constraint 'PK__#A4D1762__44A4C03D49E5E4B8'. 
Cannot insert duplicate key in object 'dbo.@changeTable'. 
The duplicate key value is (1, 1).\r\n
The data for table-valued parameter \"@changeTable\" doesn't conform to the table type of the parameter. 
SQL Server error is: 3602, state: 30\r\nThe statement has been terminated."}

Azure?

1

DDL :

CREATE TABLE [dbo].[ListItems] (
    [ListId] INT NOT NULL,
    [ItemId] INT NOT NULL,
    [Amount] INT NOT NULL, 
    [Volume] INT NOT NULL, 
    [Unit] NVARCHAR(MAX) NULL, 
    [ShelfLife] DATETIME NOT NULL, 
    CONSTRAINT [pk_ListItems] PRIMARY KEY CLUSTERED ([ShelfLife], [Volume], [Amount], [ItemId], [ListId]),
    CONSTRAINT [fk_ListItems] FOREIGN KEY ([ListId]) REFERENCES [dbo].[Lists] ([ListId]) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT [fk_ListItems2] FOREIGN KEY ([ItemId]) REFERENCES [dbo].[Items] ([ItemId]) ON DELETE CASCADE ON UPDATE CASCADE
);

Azure DDL:

CREATE TABLE [dbo].[ListItems] (
    [ListId]    INT            NOT NULL,
    [ItemId]    INT            NOT NULL,
    [Amount]    INT            NOT NULL,
    [Volume]    INT            NOT NULL,
    [Unit]      NVARCHAR (MAX) NULL,
    [ShelfLife] DATETIME       NOT NULL,
    CONSTRAINT [PK_dbo.ListItems] PRIMARY KEY CLUSTERED ([ListId] ASC, [ItemId] ASC, [Amount] ASC, [Volume] ASC, [ShelfLife] ASC),
    CONSTRAINT [FK_dbo.ListItems_dbo.Items_ItemId] FOREIGN KEY ([ItemId]) REFERENCES [dbo].[Items] ([ItemId]) ON DELETE CASCADE,
    CONSTRAINT [FK_dbo.ListItems_dbo.Lists_ListId] FOREIGN KEY ([ListId]) REFERENCES [dbo].[Lists] ([ListId]) ON DELETE CASCADE
);

PK CONSTRAINT , . ?

+4
1

, , . PK Azure ListItems ( ), . , , PK ( -):

Violation of PRIMARY KEY constraint 'PK__#A4D1762__44A4C03D49E5E4B8'. Cannot insert duplicate key in object 'dbo.@changeTable'. The duplicate key value is (1, 1).

, , "@changetable", , . , .

+1

Source: https://habr.com/ru/post/1588973/


All Articles