我需要能夠在表中插入多行的其中一個字段,外鍵將包含在多行中。在具有相同外鍵值的sql表中插入新行
目前,當我試圖做插入我得到這個錯誤:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Cannot insert duplicate key row in object 'dbo.userGroupMembership' with unique index 'IX_userId'. The duplicate key value is (264673).
查詢我使用的是:
INSERT INTO userGroupMembership(userId, usergroupId, created, adultAdminAccessLevel)
SELECT [userId], 12, GETDATE(), 0
FROM [dbo].[userGroupMembership]
where usergroupId = @UserGroupId
用戶ID是外鍵字段。
任何想法,如果我需要在表中做任何配置更改或如何能夠插入具有相同外鍵的多行?
謝謝
顯示錶格的定義。你似乎有一個不合適的'唯一'約束。 –
外鍵不是問題 - 在表 – Danny
Thx @Danny上還有一個唯一的鍵約束,我需要將'外鍵約束'設置爲否https://i.gyazo.com/42a947a87ccb7ae8374e153f6a0a4c78.png – Laziale