Add foreign key on UpgradeSchema return “Foreign key constraint is incorrectly formed”?Foreignkey integrity error while configurable product import magento 2Magento 2 foreign key constraint errorI have created an extension to show Customer Company Name in Order grid. But when creating new order, order is not showing in order griderror : add foreign keys in UpgradeSchemaMagento 2 Order Cancel foreign key constraint errorMagento 2 Add new Customer Attribute via UpgradeSchemaForeign key constraint failsError Add Foreign Key Using Install Schema Magento 2Magento 2 : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint failsMagento 2 : Foreign key constraint is incorrectly formed, ALTER TABLE customer_group

Can a stored procedure reference the database in which it is stored?

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

Where was the County of Thurn und Taxis located?

Creating a chemical industry from a medieval tech level without petroleum

How much cash can I safely carry into the USA and avoid civil forfeiture?

What is the unit of time_lock_delta in LND?

Complex numbers z=-3-4i polar form

Why do distances seem to matter in the Foundation world?

What to do with someone that cheated their way through university and a PhD program?

Restricting the options of a lookup field, based on the value of another lookup field?

A strange hotel

Does a large simulator bay have standard public address announcements?

A ​Note ​on ​N!

Multiple fireplaces in an apartment building?

How much of a wave function must reside inside event horizon for it to be consumed by the black hole?

Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?

Contradiction proof for inequality of P and NP?

Nails holding drywall

Co-worker works way more than he should

Does the damage from the Absorb Elements spell apply to your next attack, or to your first attack on your next turn?

What's the difference between using dependency injection with a container and using a service locator?

Double-nominative constructions and “von”

Negative Resistance

Find a stone which is not the lightest one



Add foreign key on UpgradeSchema return “Foreign key constraint is incorrectly formed”?


Foreignkey integrity error while configurable product import magento 2Magento 2 foreign key constraint errorI have created an extension to show Customer Company Name in Order grid. But when creating new order, order is not showing in order griderror : add foreign keys in UpgradeSchemaMagento 2 Order Cancel foreign key constraint errorMagento 2 Add new Customer Attribute via UpgradeSchemaForeign key constraint failsError Add Foreign Key Using Install Schema Magento 2Magento 2 : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint failsMagento 2 : Foreign key constraint is incorrectly formed, ALTER TABLE customer_group






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








0















I want to add a new attribute to catalog_product_website table named id_admin_user to make relation with admin_user table.



PS: I want to do that to save the ID of the admin user who added a product.



So, I create an UpgradeSchema with this code



$setup->getConnection()->addColumn(
$setup->getTable('catalog_product_website'),
'id_admin_user',
[
'type' => Table::TYPE_INTEGER,
'size' => null,
'nullable' => false,
'comment' => 'ID ADMIN USER'
]
);
$setup->getConnection()->addForeignKey(
$setup->getFkName(
'catalog_product_website',
'id_admin_user',
'admin_user',
'user_id'
),
$setup->getTable('catalog_product_website'),
'id_admin_user',

$setup->getTable('admin_user'),
'user_id',
Table::ACTION_CASCADE
);


But when I execute the setup:upgrade I get this error



SQLSTATE[HY000]: General error: 1005 Can't create table `magento_test`.`#sql-357_165b` (errno: 150 "Foreign key constraint is incorrectly formed")


is there any solution?










share|improve this question






























    0















    I want to add a new attribute to catalog_product_website table named id_admin_user to make relation with admin_user table.



    PS: I want to do that to save the ID of the admin user who added a product.



    So, I create an UpgradeSchema with this code



    $setup->getConnection()->addColumn(
    $setup->getTable('catalog_product_website'),
    'id_admin_user',
    [
    'type' => Table::TYPE_INTEGER,
    'size' => null,
    'nullable' => false,
    'comment' => 'ID ADMIN USER'
    ]
    );
    $setup->getConnection()->addForeignKey(
    $setup->getFkName(
    'catalog_product_website',
    'id_admin_user',
    'admin_user',
    'user_id'
    ),
    $setup->getTable('catalog_product_website'),
    'id_admin_user',

    $setup->getTable('admin_user'),
    'user_id',
    Table::ACTION_CASCADE
    );


    But when I execute the setup:upgrade I get this error



    SQLSTATE[HY000]: General error: 1005 Can't create table `magento_test`.`#sql-357_165b` (errno: 150 "Foreign key constraint is incorrectly formed")


    is there any solution?










    share|improve this question


























      0












      0








      0








      I want to add a new attribute to catalog_product_website table named id_admin_user to make relation with admin_user table.



      PS: I want to do that to save the ID of the admin user who added a product.



      So, I create an UpgradeSchema with this code



      $setup->getConnection()->addColumn(
      $setup->getTable('catalog_product_website'),
      'id_admin_user',
      [
      'type' => Table::TYPE_INTEGER,
      'size' => null,
      'nullable' => false,
      'comment' => 'ID ADMIN USER'
      ]
      );
      $setup->getConnection()->addForeignKey(
      $setup->getFkName(
      'catalog_product_website',
      'id_admin_user',
      'admin_user',
      'user_id'
      ),
      $setup->getTable('catalog_product_website'),
      'id_admin_user',

      $setup->getTable('admin_user'),
      'user_id',
      Table::ACTION_CASCADE
      );


      But when I execute the setup:upgrade I get this error



      SQLSTATE[HY000]: General error: 1005 Can't create table `magento_test`.`#sql-357_165b` (errno: 150 "Foreign key constraint is incorrectly formed")


      is there any solution?










      share|improve this question
















      I want to add a new attribute to catalog_product_website table named id_admin_user to make relation with admin_user table.



      PS: I want to do that to save the ID of the admin user who added a product.



      So, I create an UpgradeSchema with this code



      $setup->getConnection()->addColumn(
      $setup->getTable('catalog_product_website'),
      'id_admin_user',
      [
      'type' => Table::TYPE_INTEGER,
      'size' => null,
      'nullable' => false,
      'comment' => 'ID ADMIN USER'
      ]
      );
      $setup->getConnection()->addForeignKey(
      $setup->getFkName(
      'catalog_product_website',
      'id_admin_user',
      'admin_user',
      'user_id'
      ),
      $setup->getTable('catalog_product_website'),
      'id_admin_user',

      $setup->getTable('admin_user'),
      'user_id',
      Table::ACTION_CASCADE
      );


      But when I execute the setup:upgrade I get this error



      SQLSTATE[HY000]: General error: 1005 Can't create table `magento_test`.`#sql-357_165b` (errno: 150 "Foreign key constraint is incorrectly formed")


      is there any solution?







      magento2 upgradeschema






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 6 hours ago







      sayou

















      asked 6 hours ago









      sayousayou

      1358




      1358




















          1 Answer
          1






          active

          oldest

          votes


















          0














          In your code, setup should $setup. Check following code:



          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $installer->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );


          [Update]



          Try following way:



          $setup->getConnection()->addColumn(
          $setup->getTable('catalog_product_website'),
          'id_admin_user',
          [
          'type' => Table::TYPE_INTEGER,
          'size' => null,
          'nullable' => false,
          'unsigned' => true,
          'comment' => 'ID ADMIN USER'
          ]
          );
          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $setup->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );





          share|improve this answer

























          • Still getting the same error sir ( I replace $installer with $setup )

            – sayou
            6 hours ago












          • Check updated answer, working fine with 2.3.

            – Sohel Rana
            6 hours ago











          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%2f272484%2fadd-foreign-key-on-upgradeschema-return-foreign-key-constraint-is-incorrectly-f%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









          0














          In your code, setup should $setup. Check following code:



          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $installer->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );


          [Update]



          Try following way:



          $setup->getConnection()->addColumn(
          $setup->getTable('catalog_product_website'),
          'id_admin_user',
          [
          'type' => Table::TYPE_INTEGER,
          'size' => null,
          'nullable' => false,
          'unsigned' => true,
          'comment' => 'ID ADMIN USER'
          ]
          );
          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $setup->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );





          share|improve this answer

























          • Still getting the same error sir ( I replace $installer with $setup )

            – sayou
            6 hours ago












          • Check updated answer, working fine with 2.3.

            – Sohel Rana
            6 hours ago















          0














          In your code, setup should $setup. Check following code:



          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $installer->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );


          [Update]



          Try following way:



          $setup->getConnection()->addColumn(
          $setup->getTable('catalog_product_website'),
          'id_admin_user',
          [
          'type' => Table::TYPE_INTEGER,
          'size' => null,
          'nullable' => false,
          'unsigned' => true,
          'comment' => 'ID ADMIN USER'
          ]
          );
          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $setup->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );





          share|improve this answer

























          • Still getting the same error sir ( I replace $installer with $setup )

            – sayou
            6 hours ago












          • Check updated answer, working fine with 2.3.

            – Sohel Rana
            6 hours ago













          0












          0








          0







          In your code, setup should $setup. Check following code:



          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $installer->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );


          [Update]



          Try following way:



          $setup->getConnection()->addColumn(
          $setup->getTable('catalog_product_website'),
          'id_admin_user',
          [
          'type' => Table::TYPE_INTEGER,
          'size' => null,
          'nullable' => false,
          'unsigned' => true,
          'comment' => 'ID ADMIN USER'
          ]
          );
          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $setup->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );





          share|improve this answer















          In your code, setup should $setup. Check following code:



          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $installer->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );


          [Update]



          Try following way:



          $setup->getConnection()->addColumn(
          $setup->getTable('catalog_product_website'),
          'id_admin_user',
          [
          'type' => Table::TYPE_INTEGER,
          'size' => null,
          'nullable' => false,
          'unsigned' => true,
          'comment' => 'ID ADMIN USER'
          ]
          );
          $setup->getConnection()->addForeignKey(
          $setup->getFkName(
          'catalog_product_website',
          'id_admin_user',
          'admin_user',
          'user_id'
          ),
          $setup->getTable('catalog_product_website'),
          'id_admin_user',

          $setup->getTable('admin_user'),
          'user_id',
          Table::ACTION_CASCADE
          );






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 6 hours ago

























          answered 6 hours ago









          Sohel RanaSohel Rana

          23.6k34461




          23.6k34461












          • Still getting the same error sir ( I replace $installer with $setup )

            – sayou
            6 hours ago












          • Check updated answer, working fine with 2.3.

            – Sohel Rana
            6 hours ago

















          • Still getting the same error sir ( I replace $installer with $setup )

            – sayou
            6 hours ago












          • Check updated answer, working fine with 2.3.

            – Sohel Rana
            6 hours ago
















          Still getting the same error sir ( I replace $installer with $setup )

          – sayou
          6 hours ago






          Still getting the same error sir ( I replace $installer with $setup )

          – sayou
          6 hours ago














          Check updated answer, working fine with 2.3.

          – Sohel Rana
          6 hours ago





          Check updated answer, working fine with 2.3.

          – Sohel Rana
          6 hours ago

















          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%2f272484%2fadd-foreign-key-on-upgradeschema-return-foreign-key-constraint-is-incorrectly-f%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

          Magento 2 - Add success message with knockout Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Success / Error message on ajax request$.widget is not a function when loading a homepage after add custom jQuery on custom themeHow can bind jQuery to current document in Magento 2 When template load by ajaxRedirect page using plugin in Magento 2Magento 2 - Update quantity and totals of cart page without page reload?Magento 2: Quote data not loaded on knockout checkoutMagento 2 : I need to change add to cart success message after adding product into cart through pluginMagento 2.2.5 How to add additional products to cart from new checkout step?Magento 2 Add error/success message with knockoutCan't validate Post Code on checkout page

          Fil:Tokke komm.svg

          Where did Arya get these scars? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why did Arya refuse to end it?Has the pronunciation of Arya Stark's name changed?Has Arya forgiven people?Why did Arya Stark lose her vision?Why can Arya still use the faces?Has the Narrow Sea become narrower?Does Arya Stark know how to make poisons outside of the House of Black and White?Why did Nymeria leave Arya?Why did Arya not kill the Lannister soldiers she encountered in the Riverlands?What is the current canonical age of Sansa, Bran and Arya Stark?