Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql alter int column to bigint with foreign keys
    primarykey
    data
    text
    <p>I want to change the datatype of some primary-key columns in my database from INT to BIGINT. The following definition is a toy-example to illustrate the problem:</p> <pre class="lang-sql prettyprint-override"><code>CREATE TABLE IF NOT EXISTS `owner` ( `id` int(11) NOT NULL AUTO_INCREMENT, `thing_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `thing_id` (`thing_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; DROP TABLE IF EXISTS `thing`; CREATE TABLE IF NOT EXISTS `thing` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; ALTER TABLE `owner` ADD CONSTRAINT `owner_ibfk_1` FOREIGN KEY (`thing_id`) REFERENCES `thing` (`id`); </code></pre> <p>Now when i try to execut one of the following commands:</p> <pre class="lang-sql prettyprint-override"><code>ALTER TABLE `thing` CHANGE `id` `id` BIGINT NOT NULL AUTO_INCREMENT; ALTER TABLE `owner` CHANGE `thing_id` `thing_id` BIGINT NOT NULL; </code></pre> <p>i'm running into an error</p> <pre><code>#1025 - Error on rename of './debug/#[temp-name]' to './debug/[tablename]' (errno: 150) </code></pre> <p>SHOW INODB STATUS outputs:</p> <pre><code>LATEST FOREIGN KEY ERROR ------------------------ 120126 13:34:03 Error in foreign key constraint of table debug/owner: there is no index in the table which would contain the columns as the first columns, or the data types in the table do not match the ones in the referenced table or one of the ON ... SET NULL columns is declared NOT NULL. Constraint: , CONSTRAINT "owner_ibfk_1" FOREIGN KEY ("thing_id") REFERENCES "thing" ("id") </code></pre> <p>I'm guessing that the foreign key definition blocks changing the column type on either side. The naive approach to solve this problem would be to delete the foreign key definitions, alter the columns and re-define the foreign keys. is there a better solution?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload