Sql: cannot drop foreign key due to auto-generated constraintShow constraints on tables commandHow to add a column and make it a foreign key in single MySQL statement?How can foreign key constraints be temporarily disabled using T-SQL?Mysql error 1452 - Cannot add or update a child row: a foreign key constraint failsForce drop mysql bypassing foreign key constraintBogus foreign key constraint failMySQL DROP all tables, ignoring foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEAdd Foreign Key to existing table(Mysql Error: #1215 - Cannot add foreign key constraint) for my programCannot add foreign key constraintLaravel migration - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

When does Tiana, Ship's Caretaker check card type?

A torrent of foreign terms

Why did Saruman lie?

Is it possible to grow new organs through exposure to radioactivity?

Why does my purified Pokémon need to be healed?

My cat is a houdini

PhD advisor lost funding, need advice

Is there a standardised way to check fake news?

Is there a way to count the number of lines of text in a file including non-delimited ones?

Modeling the uncertainty of the input parameters

What is the difference between 王 and 皇?

Graphs for which a calculus student can reasonably compute the arclength

Why is statically linking glibc discouraged?

Why aren't rockets built with truss structures inside their fuel & oxidizer tanks to increase structural strength?

Split phone numbers into groups based on first digit

Running code generated in realtime in JavaScript with eval()

Update Office without opening an Office application

Flood on the top floor

Can renaming a method preserve encapsulation?

Heating Margarine in Pan = loss of calories?

What kind of liquid can be seen 'leaking' from the upper surface of the wing of a Boeing 737-800?

Translation of "I don't have anything to smile about"

How exactly are corporate bonds priced at issue

Why did IBM make public the PC BIOS source code?



Sql: cannot drop foreign key due to auto-generated constraint


Show constraints on tables commandHow to add a column and make it a foreign key in single MySQL statement?How can foreign key constraints be temporarily disabled using T-SQL?Mysql error 1452 - Cannot add or update a child row: a foreign key constraint failsForce drop mysql bypassing foreign key constraintBogus foreign key constraint failMySQL DROP all tables, ignoring foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEAdd Foreign Key to existing table(Mysql Error: #1215 - Cannot add foreign key constraint) for my programCannot add foreign key constraintLaravel migration - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails






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








0















I have a foreign key that was generated with the following command in an old and already deployed migration:



ALTER TABLE `job_template`
ADD COLUMN `parent_id` BIGINT,
ADD FOREIGN KEY fk_job_template_parent_id(parent_id) REFERENCES job_template(id) ON DELETE CASCADE;


Now I am trying to drop this foreign key with following command:



ALTER TABLE job_template DROP FOREIGN KEY fk_job_template_parent_id;


The problem is that this works for mariaDB but not for mySQL and I need a migration that would work in both cases



If I list the SHOW CREATE TABLE command (before the deleting of the foreign key) from both environments I get the following:



mariaDB:



 constraint fk_job_template_parent_id foreign key (parent_id) references job_template (id) on delete cascade,


mysql:



 constraint job_template_ibfk_5 foreign key (parent_id) references job_template (id) on delete cascade,


The constraint names are different in the 2 environments, and thus I have no way to write a migration that would consistently drop this foreign key.



Is there any way to get around this situation?










share|improve this question






























    0















    I have a foreign key that was generated with the following command in an old and already deployed migration:



    ALTER TABLE `job_template`
    ADD COLUMN `parent_id` BIGINT,
    ADD FOREIGN KEY fk_job_template_parent_id(parent_id) REFERENCES job_template(id) ON DELETE CASCADE;


    Now I am trying to drop this foreign key with following command:



    ALTER TABLE job_template DROP FOREIGN KEY fk_job_template_parent_id;


    The problem is that this works for mariaDB but not for mySQL and I need a migration that would work in both cases



    If I list the SHOW CREATE TABLE command (before the deleting of the foreign key) from both environments I get the following:



    mariaDB:



     constraint fk_job_template_parent_id foreign key (parent_id) references job_template (id) on delete cascade,


    mysql:



     constraint job_template_ibfk_5 foreign key (parent_id) references job_template (id) on delete cascade,


    The constraint names are different in the 2 environments, and thus I have no way to write a migration that would consistently drop this foreign key.



    Is there any way to get around this situation?










    share|improve this question


























      0












      0








      0








      I have a foreign key that was generated with the following command in an old and already deployed migration:



      ALTER TABLE `job_template`
      ADD COLUMN `parent_id` BIGINT,
      ADD FOREIGN KEY fk_job_template_parent_id(parent_id) REFERENCES job_template(id) ON DELETE CASCADE;


      Now I am trying to drop this foreign key with following command:



      ALTER TABLE job_template DROP FOREIGN KEY fk_job_template_parent_id;


      The problem is that this works for mariaDB but not for mySQL and I need a migration that would work in both cases



      If I list the SHOW CREATE TABLE command (before the deleting of the foreign key) from both environments I get the following:



      mariaDB:



       constraint fk_job_template_parent_id foreign key (parent_id) references job_template (id) on delete cascade,


      mysql:



       constraint job_template_ibfk_5 foreign key (parent_id) references job_template (id) on delete cascade,


      The constraint names are different in the 2 environments, and thus I have no way to write a migration that would consistently drop this foreign key.



      Is there any way to get around this situation?










      share|improve this question














      I have a foreign key that was generated with the following command in an old and already deployed migration:



      ALTER TABLE `job_template`
      ADD COLUMN `parent_id` BIGINT,
      ADD FOREIGN KEY fk_job_template_parent_id(parent_id) REFERENCES job_template(id) ON DELETE CASCADE;


      Now I am trying to drop this foreign key with following command:



      ALTER TABLE job_template DROP FOREIGN KEY fk_job_template_parent_id;


      The problem is that this works for mariaDB but not for mySQL and I need a migration that would work in both cases



      If I list the SHOW CREATE TABLE command (before the deleting of the foreign key) from both environments I get the following:



      mariaDB:



       constraint fk_job_template_parent_id foreign key (parent_id) references job_template (id) on delete cascade,


      mysql:



       constraint job_template_ibfk_5 foreign key (parent_id) references job_template (id) on delete cascade,


      The constraint names are different in the 2 environments, and thus I have no way to write a migration that would consistently drop this foreign key.



      Is there any way to get around this situation?







      mysql foreign-keys mariadb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 10:21









      Alkis MavridisAlkis Mavridis

      3882 silver badges12 bronze badges




      3882 silver badges12 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1














          Your problem is that you are not explicitly naming your constraints. This leaves each database to pick a name for you. The trick here is to name your foreign key constraints explicitly, when you create the actual tables on both MySQL and MariaDB:



          CREATE TABLE job_template (
          ...,
          parent_id int NOT NULL,
          CONSTRAINT your_constraint FOREIGN KEY fk_name (parent_id)
          REFERENCES job_template(id) ON DELETE CASCADE
          );


          But fixing your immediate situation would require more work. One option would be to query the information schema table, for the table involved, to find out the actual constraint names:



          USE INFORMATION_SCHEMA;

          SELECT
          TABLE_NAME,
          COLUMN_NAME,
          CONSTRAINT_NAME,
          REFERENCED_TABLE_NAME,
          REFERENCED_COLUMN_NAME
          FROM KEY_COLUMN_USAGE
          WHERE
          TABLE_SCHEMA = 'your_db' AND
          TABLE_NAME = 'job_template' AND
          REFERENCED_COLUMN_NAME IS NOT NULL;


          This should return one record for every column and constraint. With this information, you should be able to run your current alter statements.



          This is easy enough to do using a tool like Java, or something similar. If you want to do this directly from the database, then you would need dynamic SQL, which probably means writing a stored procedure.






          share|improve this answer



























          • Right, I will use this in the future. But can I somehow solve the current situation?

            – Alkis Mavridis
            Mar 27 at 10:32











          • That might not be so easy. You might have to query one of the information schema tables to find the actual constraint name, and then drop it. But that would take dynamic SQL.

            – Tim Biegeleisen
            Mar 27 at 10:34











          • dynamic SQL is unfortunately no option. Thanks for the answer, anyway.

            – Alkis Mavridis
            Mar 27 at 10:44











          • As I described, the create statement is already deployed and I cannot modify it. Thus, I am hoping that someone can suggest a solution or workaround for my current situation. You comment is a very good advice for the future and thanks for that, but it is not really an answer to my question.

            – Alkis Mavridis
            Mar 27 at 10:52






          • 1





            See here to get started. If you have access to something like Java, it would not be too much trouble to write a short script to handle this.

            – Tim Biegeleisen
            Mar 27 at 10:56










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f55374835%2fsql-cannot-drop-foreign-key-due-to-auto-generated-constraint%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














          Your problem is that you are not explicitly naming your constraints. This leaves each database to pick a name for you. The trick here is to name your foreign key constraints explicitly, when you create the actual tables on both MySQL and MariaDB:



          CREATE TABLE job_template (
          ...,
          parent_id int NOT NULL,
          CONSTRAINT your_constraint FOREIGN KEY fk_name (parent_id)
          REFERENCES job_template(id) ON DELETE CASCADE
          );


          But fixing your immediate situation would require more work. One option would be to query the information schema table, for the table involved, to find out the actual constraint names:



          USE INFORMATION_SCHEMA;

          SELECT
          TABLE_NAME,
          COLUMN_NAME,
          CONSTRAINT_NAME,
          REFERENCED_TABLE_NAME,
          REFERENCED_COLUMN_NAME
          FROM KEY_COLUMN_USAGE
          WHERE
          TABLE_SCHEMA = 'your_db' AND
          TABLE_NAME = 'job_template' AND
          REFERENCED_COLUMN_NAME IS NOT NULL;


          This should return one record for every column and constraint. With this information, you should be able to run your current alter statements.



          This is easy enough to do using a tool like Java, or something similar. If you want to do this directly from the database, then you would need dynamic SQL, which probably means writing a stored procedure.






          share|improve this answer



























          • Right, I will use this in the future. But can I somehow solve the current situation?

            – Alkis Mavridis
            Mar 27 at 10:32











          • That might not be so easy. You might have to query one of the information schema tables to find the actual constraint name, and then drop it. But that would take dynamic SQL.

            – Tim Biegeleisen
            Mar 27 at 10:34











          • dynamic SQL is unfortunately no option. Thanks for the answer, anyway.

            – Alkis Mavridis
            Mar 27 at 10:44











          • As I described, the create statement is already deployed and I cannot modify it. Thus, I am hoping that someone can suggest a solution or workaround for my current situation. You comment is a very good advice for the future and thanks for that, but it is not really an answer to my question.

            – Alkis Mavridis
            Mar 27 at 10:52






          • 1





            See here to get started. If you have access to something like Java, it would not be too much trouble to write a short script to handle this.

            – Tim Biegeleisen
            Mar 27 at 10:56















          1














          Your problem is that you are not explicitly naming your constraints. This leaves each database to pick a name for you. The trick here is to name your foreign key constraints explicitly, when you create the actual tables on both MySQL and MariaDB:



          CREATE TABLE job_template (
          ...,
          parent_id int NOT NULL,
          CONSTRAINT your_constraint FOREIGN KEY fk_name (parent_id)
          REFERENCES job_template(id) ON DELETE CASCADE
          );


          But fixing your immediate situation would require more work. One option would be to query the information schema table, for the table involved, to find out the actual constraint names:



          USE INFORMATION_SCHEMA;

          SELECT
          TABLE_NAME,
          COLUMN_NAME,
          CONSTRAINT_NAME,
          REFERENCED_TABLE_NAME,
          REFERENCED_COLUMN_NAME
          FROM KEY_COLUMN_USAGE
          WHERE
          TABLE_SCHEMA = 'your_db' AND
          TABLE_NAME = 'job_template' AND
          REFERENCED_COLUMN_NAME IS NOT NULL;


          This should return one record for every column and constraint. With this information, you should be able to run your current alter statements.



          This is easy enough to do using a tool like Java, or something similar. If you want to do this directly from the database, then you would need dynamic SQL, which probably means writing a stored procedure.






          share|improve this answer



























          • Right, I will use this in the future. But can I somehow solve the current situation?

            – Alkis Mavridis
            Mar 27 at 10:32











          • That might not be so easy. You might have to query one of the information schema tables to find the actual constraint name, and then drop it. But that would take dynamic SQL.

            – Tim Biegeleisen
            Mar 27 at 10:34











          • dynamic SQL is unfortunately no option. Thanks for the answer, anyway.

            – Alkis Mavridis
            Mar 27 at 10:44











          • As I described, the create statement is already deployed and I cannot modify it. Thus, I am hoping that someone can suggest a solution or workaround for my current situation. You comment is a very good advice for the future and thanks for that, but it is not really an answer to my question.

            – Alkis Mavridis
            Mar 27 at 10:52






          • 1





            See here to get started. If you have access to something like Java, it would not be too much trouble to write a short script to handle this.

            – Tim Biegeleisen
            Mar 27 at 10:56













          1












          1








          1







          Your problem is that you are not explicitly naming your constraints. This leaves each database to pick a name for you. The trick here is to name your foreign key constraints explicitly, when you create the actual tables on both MySQL and MariaDB:



          CREATE TABLE job_template (
          ...,
          parent_id int NOT NULL,
          CONSTRAINT your_constraint FOREIGN KEY fk_name (parent_id)
          REFERENCES job_template(id) ON DELETE CASCADE
          );


          But fixing your immediate situation would require more work. One option would be to query the information schema table, for the table involved, to find out the actual constraint names:



          USE INFORMATION_SCHEMA;

          SELECT
          TABLE_NAME,
          COLUMN_NAME,
          CONSTRAINT_NAME,
          REFERENCED_TABLE_NAME,
          REFERENCED_COLUMN_NAME
          FROM KEY_COLUMN_USAGE
          WHERE
          TABLE_SCHEMA = 'your_db' AND
          TABLE_NAME = 'job_template' AND
          REFERENCED_COLUMN_NAME IS NOT NULL;


          This should return one record for every column and constraint. With this information, you should be able to run your current alter statements.



          This is easy enough to do using a tool like Java, or something similar. If you want to do this directly from the database, then you would need dynamic SQL, which probably means writing a stored procedure.






          share|improve this answer















          Your problem is that you are not explicitly naming your constraints. This leaves each database to pick a name for you. The trick here is to name your foreign key constraints explicitly, when you create the actual tables on both MySQL and MariaDB:



          CREATE TABLE job_template (
          ...,
          parent_id int NOT NULL,
          CONSTRAINT your_constraint FOREIGN KEY fk_name (parent_id)
          REFERENCES job_template(id) ON DELETE CASCADE
          );


          But fixing your immediate situation would require more work. One option would be to query the information schema table, for the table involved, to find out the actual constraint names:



          USE INFORMATION_SCHEMA;

          SELECT
          TABLE_NAME,
          COLUMN_NAME,
          CONSTRAINT_NAME,
          REFERENCED_TABLE_NAME,
          REFERENCED_COLUMN_NAME
          FROM KEY_COLUMN_USAGE
          WHERE
          TABLE_SCHEMA = 'your_db' AND
          TABLE_NAME = 'job_template' AND
          REFERENCED_COLUMN_NAME IS NOT NULL;


          This should return one record for every column and constraint. With this information, you should be able to run your current alter statements.



          This is easy enough to do using a tool like Java, or something similar. If you want to do this directly from the database, then you would need dynamic SQL, which probably means writing a stored procedure.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 11:13

























          answered Mar 27 at 10:30









          Tim BiegeleisenTim Biegeleisen

          266k13 gold badges114 silver badges175 bronze badges




          266k13 gold badges114 silver badges175 bronze badges















          • Right, I will use this in the future. But can I somehow solve the current situation?

            – Alkis Mavridis
            Mar 27 at 10:32











          • That might not be so easy. You might have to query one of the information schema tables to find the actual constraint name, and then drop it. But that would take dynamic SQL.

            – Tim Biegeleisen
            Mar 27 at 10:34











          • dynamic SQL is unfortunately no option. Thanks for the answer, anyway.

            – Alkis Mavridis
            Mar 27 at 10:44











          • As I described, the create statement is already deployed and I cannot modify it. Thus, I am hoping that someone can suggest a solution or workaround for my current situation. You comment is a very good advice for the future and thanks for that, but it is not really an answer to my question.

            – Alkis Mavridis
            Mar 27 at 10:52






          • 1





            See here to get started. If you have access to something like Java, it would not be too much trouble to write a short script to handle this.

            – Tim Biegeleisen
            Mar 27 at 10:56

















          • Right, I will use this in the future. But can I somehow solve the current situation?

            – Alkis Mavridis
            Mar 27 at 10:32











          • That might not be so easy. You might have to query one of the information schema tables to find the actual constraint name, and then drop it. But that would take dynamic SQL.

            – Tim Biegeleisen
            Mar 27 at 10:34











          • dynamic SQL is unfortunately no option. Thanks for the answer, anyway.

            – Alkis Mavridis
            Mar 27 at 10:44











          • As I described, the create statement is already deployed and I cannot modify it. Thus, I am hoping that someone can suggest a solution or workaround for my current situation. You comment is a very good advice for the future and thanks for that, but it is not really an answer to my question.

            – Alkis Mavridis
            Mar 27 at 10:52






          • 1





            See here to get started. If you have access to something like Java, it would not be too much trouble to write a short script to handle this.

            – Tim Biegeleisen
            Mar 27 at 10:56
















          Right, I will use this in the future. But can I somehow solve the current situation?

          – Alkis Mavridis
          Mar 27 at 10:32





          Right, I will use this in the future. But can I somehow solve the current situation?

          – Alkis Mavridis
          Mar 27 at 10:32













          That might not be so easy. You might have to query one of the information schema tables to find the actual constraint name, and then drop it. But that would take dynamic SQL.

          – Tim Biegeleisen
          Mar 27 at 10:34





          That might not be so easy. You might have to query one of the information schema tables to find the actual constraint name, and then drop it. But that would take dynamic SQL.

          – Tim Biegeleisen
          Mar 27 at 10:34













          dynamic SQL is unfortunately no option. Thanks for the answer, anyway.

          – Alkis Mavridis
          Mar 27 at 10:44





          dynamic SQL is unfortunately no option. Thanks for the answer, anyway.

          – Alkis Mavridis
          Mar 27 at 10:44













          As I described, the create statement is already deployed and I cannot modify it. Thus, I am hoping that someone can suggest a solution or workaround for my current situation. You comment is a very good advice for the future and thanks for that, but it is not really an answer to my question.

          – Alkis Mavridis
          Mar 27 at 10:52





          As I described, the create statement is already deployed and I cannot modify it. Thus, I am hoping that someone can suggest a solution or workaround for my current situation. You comment is a very good advice for the future and thanks for that, but it is not really an answer to my question.

          – Alkis Mavridis
          Mar 27 at 10:52




          1




          1





          See here to get started. If you have access to something like Java, it would not be too much trouble to write a short script to handle this.

          – Tim Biegeleisen
          Mar 27 at 10:56





          See here to get started. If you have access to something like Java, it would not be too much trouble to write a short script to handle this.

          – Tim Biegeleisen
          Mar 27 at 10:56






          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f55374835%2fsql-cannot-drop-foreign-key-due-to-auto-generated-constraint%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript