OdbcException: ERROR [23000] [ma-3.0.3][5.7.12]Cannot add or update a child row: a foreign key constraint fails

Deadly 提交于 2019-12-11 17:37:42

问题


I am getting this error when using MariaDB Connector/ODBC with nHibernate to connect MySql database

OdbcException: ERROR [23000] [ma-3.0.3][5.7.12]Cannot add or update a child row: a foreign key constraint fails (database.ChildTable, CONSTRAINT FK_ChildTable_ParentTable FOREIGN KEY (Id) REFERENCES ParentTable (Id))

We have 2 tables, parent and child. Both tables linked with foreign key relationship. The below nHibernate mapping file.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Xxx.Yyy.Domain" assembly="Xxx.Yyy">

<class name="Xxx.Yyy.Domain.Parent, Xxx.Yyy" table="Parent" lazy="false">
   <id name="ParentId" column="ParentId" type="integer">
       <generator class="native" />
   </id>
   <property name="Name" column="Name" type="string" not-null="true"/>
   <property name="Description" column="Description" type="string" not-null="false"/>
</class>

<class name="Xxx.Yyy.Domain.Child, Xxx.Yyy" table="Child" lazy="false">
  <id name="ChildId" column="ChildId" type="integer">
      <generator class="native" />
  </id>
  <many-to-one name="Parent" not-null="true" column="ParentId" class="Xxx.Yyy.Domain.Parent, Xxx.Yyy" cascade="save-update" lazy="proxy" />
  <property name="Name" column="Name" type="string" not-null="true"/>
  <property name="Description" column="Description" type="string" not-null="false"/>
</class>

Parent records are being inserted twice while creating Parent.

  1. First record being inserted in parent table during parent creation.
  2. Second record with same details being inserted in parent table during child creation also.

But same code working fine when i am using other drivers like MySqlDataDriver. Seeing error only when using MariaDB Connector/ODBC

来源:https://stackoverflow.com/questions/50594552/odbcexception-error-23000-ma-3-0-35-7-12cannot-add-or-update-a-child-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!