1

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?

  • What's the content of your eav_entity_type table? Did you add an entity_type for you custom vendorname_modulename_entity? And does this has the id = 10? – HelgeB Feb 27 at 8:57
  • Yes. Right. I add entity_type for my module and yes it's id 10 when I generate. – Denish Feb 27 at 9:14
  • OK, what's strange is the stament which throws the error: it is an insert statement with only two values 'created_at' and 'updated_at' and the values are different. Such a statement is imposibile imho with your code since created_at and updated_at have identical values. Is there some magic interferring in your model what can manipulate your statement before saving? – HelgeB Feb 27 at 11:05
  • It returns error like : Unknown entity type: VendorName\ModuleName\Model\Mainpage requested – Denish Feb 27 at 11:20
  • Can you please tell me how to solve it? – Denish Feb 27 at 11:21
0

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 :-)

  • Yes. I added this line in ResourceModel file : $this->setType(Mainpage::ENTITY_TYPE_CODE); & ENTITY_TYPE_CODE = entity_type_code – Denish Feb 27 at 11:57
  • I have updated the answer because it's easier to read than in comments – HelgeB Feb 27 at 12:21
  • In my di.xml there are no any MetadataPool type tag. Is it required? Because, I don't need to API in my module so. – Denish Feb 27 at 12:35
  • Sorry. But, I'm new in create eav entity type module. – Denish Feb 27 at 12:36
  • "Unknown entity type: VendorName\ModuleName\Model\Mainpage requested" comes from the getMetadata() method in the EntityManagers MetadataPool, that's all I can guess from what you have posted - I don't know anything about your code. So try to debug where the calls come from, it doesn't make sense to spend a lot of time trying to guess your code :-) – HelgeB Feb 27 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.