nwoltman/node-mysql-plus

db sync alter order

sanyatuning opened this issue · 4 comments

Hi

I have a table "build", with a column "buildId" PK and auto increment.
I would like to change it to be not an auto incerement field, but i got an error.

====== mysql-plus operations: ======

type: DROP_KEY
SQL: ALTER TABLE `build` DROP PRIMARY KEY

type: MODIFY_COLUMN
SQL: ALTER TABLE `build` MODIFY COLUMN `buildId` int unsigned NOT NULL

type: ADD_KEY
SQL: ALTER TABLE `build` ADD PRIMARY KEY (`app`, `buildId`)

====================================


====== mysql-plus sync errored on operation: ======

type: DROP_KEY
SQL: ALTER TABLE `build` DROP PRIMARY KEY

====================================

db.sync ERROR { Error: ER_WRONG_AUTO_KEY: Incorrect table definition; there can be only one auto column and it must be defined as a key
    at Query.Sequence._packetToError (/home/.../server/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
    at Query.ErrorPacket (/home/.../server/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
    at Protocol._parsePacket (/home/.../server/node_modules/mysql/lib/protocol/Protocol.js:280:23)
    at Parser.write (/home/.../server/node_modules/mysql/lib/protocol/Parser.js:75:12)
    at Protocol.write (/home/.../server/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/home/.../server/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Socket.EventEmitter.emit (/home/.../server/node_modules/sc-domain/index.js:12:31)
    at readableAddChunk (_stream_readable.js:176:18)
    --------------------
    at Protocol._enqueue (/home/.../server/node_modules/mysql/lib/protocol/Protocol.js:141:48)
    at PoolConnection.query (/home/.../server/node_modules/mysql/lib/Connection.js:208:25)
    at getConnection (/home/.../server/node_modules/mysql-plus/lib/PoolPlus.js:369:22)
    at Ping.onOperationComplete [as _callback] (/home/.../server/node_modules/mysql/lib/Pool.js:110:5)
    at Ping.Sequence.end (/home/.../server/node_modules/mysql/lib/protocol/sequences/Sequence.js:86:24)
    at Ping.Sequence.OkPacket (/home/.../server/node_modules/mysql/lib/protocol/sequences/Sequence.js:95:8)
    at Protocol._parsePacket (/home/.../server/node_modules/mysql/lib/protocol/Protocol.js:280:23)
    at Parser.write (/home/.../server/node_modules/mysql/lib/protocol/Parser.js:75:12)
    at Protocol.write (/home/.../server/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/home/.../server/node_modules/mysql/lib/Connection.js:103:28)
  code: 'ER_WRONG_AUTO_KEY',
  errno: 1075,
  sqlState: '42000',
  index: 0 }

npm ERR! Linux 4.2.0-42-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "dev"
npm ERR! node v6.9.5
npm ERR! npm  v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! stork.js@0.1.0 dev: `ts-node src/app.ts --dev`
npm ERR! Exit status 1
npm ERR! 

I think the order of operations is incorrect.
Maybe it would be better to merge alter queries like this:
(This query works fine for me.)

ALTER TABLE `build` 
    DROP PRIMARY KEY,
    MODIFY COLUMN `buildId` int unsigned NOT NULL,
    ADD PRIMARY KEY (`app`, `buildId`)

Thanks for the bug report @sanyatuning! I am able to reproduce the bug and will start working on a fix for it.

Merging alter queries into a single statement sounds like a great solution, however, that will take a decent amount of code restructuring and I'm not sure how easy debugging will be with merged alter queries, so I'll take a look into that a little later. For now I think I can put out a fix for this fairly quickly using a different method.

Sorry, disregard that last part. Using a single alter statement is actually easier to implement :)

Thanks for the fast reply.

Fixed in v0.7.0 :)