OdbcException: ERROR [23000] [ma-3.0.3][5.7.12]Can

2019-08-27 22:32发布

问题:

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