Adding foreign key to existing table gives error 1050 table already exists

后端 未结 9 1088
栀梦
栀梦 2021-01-07 18:11

I\'ve a table CustomizationSet with the columns:

customization_set_guid (which is a non-nullable guid and also the primary key)
creator_account_guid
and a fe         


        
相关标签:
9条回答
  • 2021-01-07 18:38

    Not sure about the table already existing, but the reason it's not letting you choose the column you want is most likely due to the columns not being the same type. Check to ensure they are both the same type, same length, and have all the same options.

    0 讨论(0)
  • 2021-01-07 18:39

    I got the same error, and since my case wasnt mentioned yet, i ll post this answer and hopefully it may save somebody's time!

    My two tables engines, where different. The one was InnoDB, and the other MyIsam.

    To change the engine of a table:

    choose table, hit alter table, and then to hit that double arrow at the right-most of the Workbench(so it will point upwards).

    Now change the engine!

    0 讨论(0)
  • 2021-01-07 18:39

    When using MysqlWorkbench the error is misleading. My issue was that I was trying to add a foreign key constraint on table that already had rows and one of the rows was empty (did not meet the FK constraint. Instead of complaining that constraint will fail if applied, MysqlWorkbench reported that table exists.

    Removing the offending row fixed (or adding and constraint acceptable value to the field) solved the problem.

    0 讨论(0)
  • 2021-01-07 18:41

    I'm not sure if it's a typo but shouldn't be

    ALTER TABLE Registration ADD FOREIGN KEY 
    (
        customization_set_guid
    ) REFERENCES CustomizationSet (
        customization_set_guid
    );
    

    be something like

    ALTER TABLE Registration ADD FOREIGN KEY 
    customization_set_guid_fk (customization_set_guid) 
    REFERENCES CustomizationSet (customization_set_guid);
    
    0 讨论(0)
  • 2021-01-07 18:42

    I got the same error, and it was due to the fact that the foreign key already existed. What you want is just to add the constraint:

    ALTER TABLE Registration 
      ADD CONSTRAINT idx_Registration_CustomizationSet 
      FOREIGN KEY (customization_set_guid) 
      REFERENCES CustomizationSet(customization_set_guid);
    
    0 讨论(0)
  • 2021-01-07 18:50
    • Check the Storage Engine type for CustomizationSet table.

    I had a same issue but i could solve it by changing engine type to InnoDB , because few types don't support foreign key constraints.

    0 讨论(0)
提交回复
热议问题