Magento 2 : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint...
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
, CONSTRAINTVENDORNAME_MODULENAME_ENTT_ENTT_TYPE_ID_EAV_ENTT_TYPE_ENTT_TYPE_ID
FOREIGN KEY (entity_type_id
) REFERENCESeav_entity_type
(entity_type_id
) ), query was: INSERT INTOvendorname_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 MagentoBackendModelViewResultRedirect $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?
magento2 error
add a comment |
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
, CONSTRAINTVENDORNAME_MODULENAME_ENTT_ENTT_TYPE_ID_EAV_ENTT_TYPE_ENTT_TYPE_ID
FOREIGN KEY (entity_type_id
) REFERENCESeav_entity_type
(entity_type_id
) ), query was: INSERT INTOvendorname_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 MagentoBackendModelViewResultRedirect $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?
magento2 error
add a comment |
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
, CONSTRAINTVENDORNAME_MODULENAME_ENTT_ENTT_TYPE_ID_EAV_ENTT_TYPE_ENTT_TYPE_ID
FOREIGN KEY (entity_type_id
) REFERENCESeav_entity_type
(entity_type_id
) ), query was: INSERT INTOvendorname_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 MagentoBackendModelViewResultRedirect $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?
magento2 error
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
, CONSTRAINTVENDORNAME_MODULENAME_ENTT_ENTT_TYPE_ID_EAV_ENTT_TYPE_ENTT_TYPE_ID
FOREIGN KEY (entity_type_id
) REFERENCESeav_entity_type
(entity_type_id
) ), query was: INSERT INTOvendorname_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 MagentoBackendModelViewResultRedirect $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?
magento2 error
magento2 error
asked 2 mins ago
DenishDenish
817
817
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263579%2fmagento-2-integrity-constraint-violation-1452-cannot-add-or-update-a-child-ro%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263579%2fmagento-2-integrity-constraint-violation-1452-cannot-add-or-update-a-child-ro%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown