Lỗi the insert statement conflicted with the foreign key constraint năm 2024

When inserting data containing a nested CDT, an error in the UI or on the Alerts tab in the Designer Interface, such as the following, is generated:

Show

An error occurred while trying to write to the entity “XXXX” [id=9756441a-16e3-45f9-bb5e-957fd04cdd51@217, type=XXXX (id=8266)] (data store: XXXX). Details: org.hibernate.exception.ConstraintViolationException: could not insert: [XXXX]: com.microsoft.sqlserver.jdbc.SQLServerException: The INSERT statement conflicted with the FOREIGN KEY constraint "XXXX". The conflict occurred in database "XXXX", table "dbo.XXXX", column 'XXXX'.

Note that this error is specific to SQL Server, but similar errors can also occur in other supported databases. In the database the tables are created in a manner similar to the following:

  • The parent table has a primary key with a one-to-many relationship to the same key in the child table.
  • The child table has the above key as a foreign key.

Cause

It is likely that the data for the child table with the foreign key is being added before the data for the parent table with the primary key. A foreign key cannot have a value in its column that is not present in the primary key of the parent table.

Additionally, it's possible that there is a logic error in the expression inserting the data. When inserting data that is blank for the foreign key, a NULL value should be used rather than an empty value. The error message will contain the data that is being passed to the database. A series of empty values, which could cause this issue, would look like this:

Data: TypedValue[it=8266,v={X,{{,,}}}]

NULL, which should be used in place of empty values, would look similar to this:

Data: TypedValue[it=8266,v={X,{{,,}}}]

Action

Make sure the table containing the primary key is built first, followed by the table with the foreign key. If inserting the data with both a primary key and foreign key present, make sure the data for the primary key is inserted prior to the data for the foreign key. If inserting blank data use NULL rather than empty values.

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_PHANCONG_NHANVIEN". The conflict occurred in database "quanlinhansu", table "dbo.NHANVIEN", column 'manv'.

Bình luận

Lỗi the insert statement conflicted with the foreign key constraint năm 2024

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập

Lỗi the insert statement conflicted with the foreign key constraint năm 2024

emenpyđã bình luận 05:27 07-04-2017

Mình mới bắt đầu học WPF và đã xem qua tuts WPF của Kteam mà ko thấy có hướng dẫn cách tạo custom control. Mình muốn tạo 1 cái custom control trong đó có Image thay đổi theo status change. Vd: khi giá trị là true thì hiện icon bubble màu xanh còn nếu là false thì hiện icon màu đỏ. Làm sao để thay đổi image source với dependencyproperty? Bạn có thể làm clip hướng dẫn cụ thể cách làm custom control đc ko? Cám ơn bạn nhiều lắm!!

Ps: tại sao mình tạo câu hỏi ko đc nhỉ? cứ bị error

Lỗi the insert statement conflicted with the foreign key constraint năm 2024

K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 17:32 06-04-2017

Cái này có nghĩa là khóa ngoại của bạn nó trùng hoặc không tồn tại giá trị tương ứng của khóa chính trong bảng kia

To add to what Richard has said, a foreign key is a way to tell SQL that two tables are related: a value in a Confectionary table row is related to a single value in the Category table, in much the same way as an Invoice Line describing a sold product is related by a Foreign key to the Invoices table by the Invoice Number: an invoice will describe several sold items, but there is only ever one Invoice related to that entire group of Invoice Lines.

A foreign key can only be set to a value that exists in the other table, or your data become inconsistent: you have to create the Invoice before you add lines to it!

Similarly, your Category needs to be setup before you start adding items to the Confectionary table that refer to them.