Microsoft Dynamics AX Support

This blog contains posts by the Microsoft Dynamics AX Support Teams Worldwide

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ModelElementData_HasModelId_LayerId". The conflict occurred in database "YourDataBaseName_model", table "dbo.Model"

Given scenario:

You receive the following failure when compiling the application:

The INSERT statement conflicted with the FOREIGN KEY constraint “FK_ModelElementData_HasModelId_LayerId”. The conflict occurred in database “YourDataBaseName_model”, table “dbo.Model”

 

Cause:

This might be caused by some metadata pieces that have offending <LayerId, ModelId> combination)

 

Resolution suggestion:

a. Disable the FK constraint from model database by running following SQL statement:

 EXEC sp_msforeachtable “ALTER TABLE ? NOCHECK CONSTRAINT all”

b. Do a full application compile

c. Run the following SQL query to check the affected objects :

 

select top 100 *from ModelElementData med

where not exists

( select 1 from UTILMODELS um where med.ModelId=um.ID and med.LayerId=Layer)

d. Take necessary actions to correct the affected objects 

e.  Enable the FK constraint again on your Model Database by running following SQL statement:

EXEC sp_msforeachtable “ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all”

f. Do a full XPP compilation and a full CIL generation