Magento – Magento 2 : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

errormagento2

When I save records, I got this error message.

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(report.vendorname_modulename_entity, CONSTRAINT
VENDORNAME_MODULENAME_ENTT_ENTT_TYPE_ID_EAV_ENTT_TYPE_ENTT_TYPE_ID
FOREIGN KEY (entity_type_id) REFERENCES eav_entity_type
(entity_type_id) ), query was: INSERT INTO
vendorname_modulename_entity (created_at,updated_at) VALUES
('2019-02-26 00:00:00', '2019-02-27 05:12:31')

Save controller file (execute function):

$storeId = (int) $this->getRequest()->getParam('store_id', 0);
$data = $this->getRequest()->getParams();

/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($data) {
    $params = [];
    $model = $this->modelFactory->create();
    $params['store'] = $storeId;
    if (empty($data['entity_id'])) {
        $data['entity_id'] = null;
    } else {
        $model->load($data['entity_id']);
        $params['entity_id'] = $data['entity_id'];
    }
    $model->setData('entity_type_id', 10);
    $model->setData('attribute_set_id', 17);
    $model->setData('store_id', $storeId);
    $model->setData('field_title', $data['field_title']);
    $model->setData('field_url', $data['field_url']);
    $model->setData('field_desc', $data['field_desc']);
    $model->setData('created_at', $this->_dateFactory->create()->date());
    $model->setData('updated_at', $this->_dateFactory->create()->date());

    try {
        $model->save();
        $this->messageManager->addSuccessMessage(__('You saved this record.'));
        $this->_getSession()->setFormData(false);
        if ($this->getRequest()->getParam('back')) {
            $params['entity_id'] = $model->getId();
            $params['_current'] = true;
            return $resultRedirect->setPath('*/*/edit', $params);
        }
        return $resultRedirect->setPath('*/*/');
    } catch (\Exception $e) {
        $this->messageManager->addErrorMessage($e->getMessage());
        $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the record.'));
    }

    $this->_getSession()->setFormData($this->getRequest()->getPostValue());
    return $resultRedirect->setPath('*/*/edit', $params);
}
return $resultRedirect->setPath('*/*/');

How I solve this error?

Best Answer

Without the specific code it is a little hard to guess but "unknown entity type" could be because of missing value for the entity type in the resource model you have created or a incomplete / incorrect settings in di.xml

Resource model needs the entity type set - something like this usually in constructor (you checked that, it's there in your case): $this->setType('your_entity_type_code');

"Unknown entity type: VendorName\ModuleName\Model\Mainpage requested" comes from the getMetadata() method in the EntityManagers MetadataPool. The di.xml needs a tag for Magento\Framework\EntityManager\MetadataPool to avoid this error

<type name="Magento\Framework\EntityManager\MetadataPool">
  [....]
  <item name="eavEntityType" xsi:type="string">your_entity_type_code</item>

If the di.xml is correct and you have generated the dependency injection after the changes, we have to look further :-)

Related Topic