Update record error : “Unique constraint violation found” in magento2How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existAdd product programmatically magento 2: Unique constraint violation foundMagento 2.2.1: Add Custom Upload file attribute in CheckoutAdd categories automatically - Unique constraint violation foundMagento 2.2.5: Add, Update and Delete existing products Custom OptionsUnique constraint violation found magento 2Unique constraint violation found on “catalog_product_entity_tier_price”Can't update product / Unique constraint violation

How would you say "Sorry, that was a mistake on my part"?

How to check if a new username is a system user?

Cauchy reals and Dedekind reals satisfy "the same mathematical theorems"

ArcPy Delete Function not working inside for loop?

What is the difference between a Hosaka, Ono-Sendai, and a "deck"?

How to color a tag in a math equation?

How many bits in the resultant hash will change, if the x bits are changed in its the original input?

Where do the electrons come from to make the carbon stable during bombardment of alpha particles on beryllium

What does it actually mean to have two time dimensions?

Why did Steve Rogers choose this character in Endgame?

Interviewing with an unmentioned 9 months of sick leave taken during a job

How can electric field be defined as force per charge, if the charge makes its own, singular electric field?

Wordplay subtraction paradox

How can I help our ranger feel special about her beast companion?

How Can I Process Untrusted Data Sources Securely?

Why is the total probability theorem expressed in this way?

How should one refer to knights (& dames) in academic writing?

Is passive Investigation essentially truesight against illusions?

Why don't commercial aircraft adopt a slightly more seaplane-like design to allow safer ditching in case of emergency?

Why is Katakana not pronounced Katagana?

Alphanumeric Line and Curve Counting

Is it ethical for a company to ask its employees to move furniture on a weekend?

Optimising the Selection of MaxValue in Association

Wordplay addition paradox



Update record error : “Unique constraint violation found” in magento2


How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existAdd product programmatically magento 2: Unique constraint violation foundMagento 2.2.1: Add Custom Upload file attribute in CheckoutAdd categories automatically - Unique constraint violation foundMagento 2.2.5: Add, Update and Delete existing products Custom OptionsUnique constraint violation found magento 2Unique constraint violation found on “catalog_product_entity_tier_price”Can't update product / Unique constraint violation






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















In the controller, I am inserting and updating data.
But when record is already exist it's not update it's again try to insert so i am getting above error:



if(!$checkoutPrescriptionCollection->count()) 
$checkoutPrescription->setQuoteId($quoteId);
$checkoutPrescription->setCustomerPrescriptionId($prescription);
$checkoutPrescription->save();

else
$checkoutPrescription->load($quoteId,'quote_id');
if($checkoutPrescription->getPrescriptionQuoteId())
$checkoutPrescription->setCustomerPrescriptionId($prescription);
$checkoutPrescription->save();




In xml file



<?xml version="1.0"?>
<!--
/*
* Copyright © 2019 vendor. All rights reserved.
* See COPYING.txt for license details
*/
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table comment="Customer Prescption Quote Table" engine="innodb" name="vendor_prescription_quote" resource="default">
<column comment="Prescription Quote Id" identity="true" name="prescription_quote_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<column comment="Quote Id" name="quote_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<column comment="Prescription Id" name="customer_prescription_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<constraint referenceId="PRIMARY" xsi:type="primary">
<column name="prescription_quote_id"/>
</constraint>
<!-- <constraint xsi:type="foreign" referenceId="CUSTOMER_PRESCRIPTION_ID" table="vendor_prescription_quote" column="customer_prescription_id" referenceTable="customer_prescription" referenceColumn="customer_prescription_id"/> -->
</table>
</schema>









share|improve this question
























  • can you show error screenshot and installSchema.php file ?

    – Mohit Rane
    Jul 9 at 6:26











  • @MohitRane please check

    – Rutvee Sojitra
    Jul 9 at 6:28











  • you didn't mention constraint for quote_id and customer_prescription_id

    – Mohit Rane
    Jul 9 at 6:37











  • what are you using to insert/modify data, model or factory ?

    – Mohit Rane
    Jul 9 at 7:57











  • I had done the same in my module, i can make my answer useful if you could post your code.

    – Mohit Rane
    Jul 9 at 8:03

















1















In the controller, I am inserting and updating data.
But when record is already exist it's not update it's again try to insert so i am getting above error:



if(!$checkoutPrescriptionCollection->count()) 
$checkoutPrescription->setQuoteId($quoteId);
$checkoutPrescription->setCustomerPrescriptionId($prescription);
$checkoutPrescription->save();

else
$checkoutPrescription->load($quoteId,'quote_id');
if($checkoutPrescription->getPrescriptionQuoteId())
$checkoutPrescription->setCustomerPrescriptionId($prescription);
$checkoutPrescription->save();




In xml file



<?xml version="1.0"?>
<!--
/*
* Copyright © 2019 vendor. All rights reserved.
* See COPYING.txt for license details
*/
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table comment="Customer Prescption Quote Table" engine="innodb" name="vendor_prescription_quote" resource="default">
<column comment="Prescription Quote Id" identity="true" name="prescription_quote_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<column comment="Quote Id" name="quote_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<column comment="Prescription Id" name="customer_prescription_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<constraint referenceId="PRIMARY" xsi:type="primary">
<column name="prescription_quote_id"/>
</constraint>
<!-- <constraint xsi:type="foreign" referenceId="CUSTOMER_PRESCRIPTION_ID" table="vendor_prescription_quote" column="customer_prescription_id" referenceTable="customer_prescription" referenceColumn="customer_prescription_id"/> -->
</table>
</schema>









share|improve this question
























  • can you show error screenshot and installSchema.php file ?

    – Mohit Rane
    Jul 9 at 6:26











  • @MohitRane please check

    – Rutvee Sojitra
    Jul 9 at 6:28











  • you didn't mention constraint for quote_id and customer_prescription_id

    – Mohit Rane
    Jul 9 at 6:37











  • what are you using to insert/modify data, model or factory ?

    – Mohit Rane
    Jul 9 at 7:57











  • I had done the same in my module, i can make my answer useful if you could post your code.

    – Mohit Rane
    Jul 9 at 8:03













1












1








1








In the controller, I am inserting and updating data.
But when record is already exist it's not update it's again try to insert so i am getting above error:



if(!$checkoutPrescriptionCollection->count()) 
$checkoutPrescription->setQuoteId($quoteId);
$checkoutPrescription->setCustomerPrescriptionId($prescription);
$checkoutPrescription->save();

else
$checkoutPrescription->load($quoteId,'quote_id');
if($checkoutPrescription->getPrescriptionQuoteId())
$checkoutPrescription->setCustomerPrescriptionId($prescription);
$checkoutPrescription->save();




In xml file



<?xml version="1.0"?>
<!--
/*
* Copyright © 2019 vendor. All rights reserved.
* See COPYING.txt for license details
*/
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table comment="Customer Prescption Quote Table" engine="innodb" name="vendor_prescription_quote" resource="default">
<column comment="Prescription Quote Id" identity="true" name="prescription_quote_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<column comment="Quote Id" name="quote_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<column comment="Prescription Id" name="customer_prescription_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<constraint referenceId="PRIMARY" xsi:type="primary">
<column name="prescription_quote_id"/>
</constraint>
<!-- <constraint xsi:type="foreign" referenceId="CUSTOMER_PRESCRIPTION_ID" table="vendor_prescription_quote" column="customer_prescription_id" referenceTable="customer_prescription" referenceColumn="customer_prescription_id"/> -->
</table>
</schema>









share|improve this question
















In the controller, I am inserting and updating data.
But when record is already exist it's not update it's again try to insert so i am getting above error:



if(!$checkoutPrescriptionCollection->count()) 
$checkoutPrescription->setQuoteId($quoteId);
$checkoutPrescription->setCustomerPrescriptionId($prescription);
$checkoutPrescription->save();

else
$checkoutPrescription->load($quoteId,'quote_id');
if($checkoutPrescription->getPrescriptionQuoteId())
$checkoutPrescription->setCustomerPrescriptionId($prescription);
$checkoutPrescription->save();




In xml file



<?xml version="1.0"?>
<!--
/*
* Copyright © 2019 vendor. All rights reserved.
* See COPYING.txt for license details
*/
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table comment="Customer Prescption Quote Table" engine="innodb" name="vendor_prescription_quote" resource="default">
<column comment="Prescription Quote Id" identity="true" name="prescription_quote_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<column comment="Quote Id" name="quote_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<column comment="Prescription Id" name="customer_prescription_id" nullable="false" padding="10" unsigned="true" xsi:type="int"/>
<constraint referenceId="PRIMARY" xsi:type="primary">
<column name="prescription_quote_id"/>
</constraint>
<!-- <constraint xsi:type="foreign" referenceId="CUSTOMER_PRESCRIPTION_ID" table="vendor_prescription_quote" column="customer_prescription_id" referenceTable="customer_prescription" referenceColumn="customer_prescription_id"/> -->
</table>
</schema>






magento2 checkout controllers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 9 at 6:41









MSA

1,4063 silver badges17 bronze badges




1,4063 silver badges17 bronze badges










asked Jul 9 at 6:20









Rutvee SojitraRutvee Sojitra

2,2941 gold badge4 silver badges22 bronze badges




2,2941 gold badge4 silver badges22 bronze badges












  • can you show error screenshot and installSchema.php file ?

    – Mohit Rane
    Jul 9 at 6:26











  • @MohitRane please check

    – Rutvee Sojitra
    Jul 9 at 6:28











  • you didn't mention constraint for quote_id and customer_prescription_id

    – Mohit Rane
    Jul 9 at 6:37











  • what are you using to insert/modify data, model or factory ?

    – Mohit Rane
    Jul 9 at 7:57











  • I had done the same in my module, i can make my answer useful if you could post your code.

    – Mohit Rane
    Jul 9 at 8:03

















  • can you show error screenshot and installSchema.php file ?

    – Mohit Rane
    Jul 9 at 6:26











  • @MohitRane please check

    – Rutvee Sojitra
    Jul 9 at 6:28











  • you didn't mention constraint for quote_id and customer_prescription_id

    – Mohit Rane
    Jul 9 at 6:37











  • what are you using to insert/modify data, model or factory ?

    – Mohit Rane
    Jul 9 at 7:57











  • I had done the same in my module, i can make my answer useful if you could post your code.

    – Mohit Rane
    Jul 9 at 8:03
















can you show error screenshot and installSchema.php file ?

– Mohit Rane
Jul 9 at 6:26





can you show error screenshot and installSchema.php file ?

– Mohit Rane
Jul 9 at 6:26













@MohitRane please check

– Rutvee Sojitra
Jul 9 at 6:28





@MohitRane please check

– Rutvee Sojitra
Jul 9 at 6:28













you didn't mention constraint for quote_id and customer_prescription_id

– Mohit Rane
Jul 9 at 6:37





you didn't mention constraint for quote_id and customer_prescription_id

– Mohit Rane
Jul 9 at 6:37













what are you using to insert/modify data, model or factory ?

– Mohit Rane
Jul 9 at 7:57





what are you using to insert/modify data, model or factory ?

– Mohit Rane
Jul 9 at 7:57













I had done the same in my module, i can make my answer useful if you could post your code.

– Mohit Rane
Jul 9 at 8:03





I had done the same in my module, i can make my answer useful if you could post your code.

– Mohit Rane
Jul 9 at 8:03










1 Answer
1






active

oldest

votes


















-1














You need to add foreign key constraint for quote_id and customer_prescription_id



Refer this link




https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html




<constraint xsi:type="foreign" referenceId="FL_ALLOWED_SEVERITIES" table="declarative_table" column="severity" referenceTable="severities" referenceColumn="severity_identifier" onDelete="CASCADE"/>





share|improve this answer

























  • why need to give foreign key reference ? it should be update right?

    – Rutvee Sojitra
    Jul 9 at 7:15











  • are you using quote_id as reference to Magento default table?

    – Mohit Rane
    Jul 9 at 7:16











  • yes but i am updating only customer_prescription_id

    – Rutvee Sojitra
    Jul 9 at 7:17











  • you're also setting the $quote_id value, $checkoutPrescription->setQuoteId($quoteId);

    – Mohit Rane
    Jul 9 at 7:22













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



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f281294%2fupdate-record-error-unique-constraint-violation-found-in-magento2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














You need to add foreign key constraint for quote_id and customer_prescription_id



Refer this link




https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html




<constraint xsi:type="foreign" referenceId="FL_ALLOWED_SEVERITIES" table="declarative_table" column="severity" referenceTable="severities" referenceColumn="severity_identifier" onDelete="CASCADE"/>





share|improve this answer

























  • why need to give foreign key reference ? it should be update right?

    – Rutvee Sojitra
    Jul 9 at 7:15











  • are you using quote_id as reference to Magento default table?

    – Mohit Rane
    Jul 9 at 7:16











  • yes but i am updating only customer_prescription_id

    – Rutvee Sojitra
    Jul 9 at 7:17











  • you're also setting the $quote_id value, $checkoutPrescription->setQuoteId($quoteId);

    – Mohit Rane
    Jul 9 at 7:22















-1














You need to add foreign key constraint for quote_id and customer_prescription_id



Refer this link




https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html




<constraint xsi:type="foreign" referenceId="FL_ALLOWED_SEVERITIES" table="declarative_table" column="severity" referenceTable="severities" referenceColumn="severity_identifier" onDelete="CASCADE"/>





share|improve this answer

























  • why need to give foreign key reference ? it should be update right?

    – Rutvee Sojitra
    Jul 9 at 7:15











  • are you using quote_id as reference to Magento default table?

    – Mohit Rane
    Jul 9 at 7:16











  • yes but i am updating only customer_prescription_id

    – Rutvee Sojitra
    Jul 9 at 7:17











  • you're also setting the $quote_id value, $checkoutPrescription->setQuoteId($quoteId);

    – Mohit Rane
    Jul 9 at 7:22













-1












-1








-1







You need to add foreign key constraint for quote_id and customer_prescription_id



Refer this link




https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html




<constraint xsi:type="foreign" referenceId="FL_ALLOWED_SEVERITIES" table="declarative_table" column="severity" referenceTable="severities" referenceColumn="severity_identifier" onDelete="CASCADE"/>





share|improve this answer















You need to add foreign key constraint for quote_id and customer_prescription_id



Refer this link




https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html




<constraint xsi:type="foreign" referenceId="FL_ALLOWED_SEVERITIES" table="declarative_table" column="severity" referenceTable="severities" referenceColumn="severity_identifier" onDelete="CASCADE"/>






share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 9 at 7:21

























answered Jul 9 at 7:05









Mohit RaneMohit Rane

63014 bronze badges




63014 bronze badges












  • why need to give foreign key reference ? it should be update right?

    – Rutvee Sojitra
    Jul 9 at 7:15











  • are you using quote_id as reference to Magento default table?

    – Mohit Rane
    Jul 9 at 7:16











  • yes but i am updating only customer_prescription_id

    – Rutvee Sojitra
    Jul 9 at 7:17











  • you're also setting the $quote_id value, $checkoutPrescription->setQuoteId($quoteId);

    – Mohit Rane
    Jul 9 at 7:22

















  • why need to give foreign key reference ? it should be update right?

    – Rutvee Sojitra
    Jul 9 at 7:15











  • are you using quote_id as reference to Magento default table?

    – Mohit Rane
    Jul 9 at 7:16











  • yes but i am updating only customer_prescription_id

    – Rutvee Sojitra
    Jul 9 at 7:17











  • you're also setting the $quote_id value, $checkoutPrescription->setQuoteId($quoteId);

    – Mohit Rane
    Jul 9 at 7:22
















why need to give foreign key reference ? it should be update right?

– Rutvee Sojitra
Jul 9 at 7:15





why need to give foreign key reference ? it should be update right?

– Rutvee Sojitra
Jul 9 at 7:15













are you using quote_id as reference to Magento default table?

– Mohit Rane
Jul 9 at 7:16





are you using quote_id as reference to Magento default table?

– Mohit Rane
Jul 9 at 7:16













yes but i am updating only customer_prescription_id

– Rutvee Sojitra
Jul 9 at 7:17





yes but i am updating only customer_prescription_id

– Rutvee Sojitra
Jul 9 at 7:17













you're also setting the $quote_id value, $checkoutPrescription->setQuoteId($quoteId);

– Mohit Rane
Jul 9 at 7:22





you're also setting the $quote_id value, $checkoutPrescription->setQuoteId($quoteId);

– Mohit Rane
Jul 9 at 7:22

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f281294%2fupdate-record-error-unique-constraint-violation-found-in-magento2%23new-answer', 'question_page');

);

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







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?