Skip to content

Bug when trying to add unique index from node-orm2 #23

Description

@BobNobrain

Using node-orm2 and this model definition:

db.define('user', {
    email:          { type: 'text', size: 40, required: true, unique: 'email' }
});

After calling db.syncPromise() I get this error:

{ Error: ER_NO_SUCH_TABLE: Table 'shop.undefined' doesn't exist
    at Query.Sequence._packetToError (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
    at Query.ErrorPacket (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
    at Protocol._parsePacket (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)
    --------------------
    at Protocol._enqueue (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at PoolConnection.query (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/Connection.js:208:25)
    at /home/bob/git/study/node-shop/server/node_modules/orm/lib/Drivers/DML/mysql.js:231:9
    at Handshake.onConnect (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/Pool.js:64:7)
    at Handshake.Sequence.end (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
    at Handshake.Sequence.OkPacket (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:97:8)
    at Protocol._parsePacket (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)
  cause: 
   { Error: ER_NO_SUCH_TABLE: Table 'shop.undefined' doesn't exist
       at Query.Sequence._packetToError (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
       at Query.ErrorPacket (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
       at Protocol._parsePacket (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:279:23)
       at Parser.write (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Parser.js:76:12)
       at Protocol.write (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:39:16)
       at Socket.<anonymous> (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/Connection.js:103:28)
       at emitOne (events.js:96:13)
       at Socket.emit (events.js:188:7)
       at readableAddChunk (_stream_readable.js:176:18)
       at Socket.Readable.push (_stream_readable.js:134:10)
       at TCP.onread (net.js:548:20)
       --------------------
       at Protocol._enqueue (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:145:48)
       at PoolConnection.query (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/Connection.js:208:25)
       at /home/bob/git/study/node-shop/server/node_modules/orm/lib/Drivers/DML/mysql.js:231:9
       at Handshake.onConnect (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/Pool.js:64:7)
       at Handshake.Sequence.end (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
       at Handshake.Sequence.OkPacket (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/sequences/Sequence.js:97:8)
       at Protocol._parsePacket (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:279:23)
       at Parser.write (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Parser.js:76:12)
       at Protocol.write (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/protocol/Protocol.js:39:16)
       at Socket.<anonymous> (/home/bob/git/study/node-shop/server/node_modules/mysql/lib/Connection.js:103:28)
       at emitOne (events.js:96:13)
       at Socket.emit (events.js:188:7)
       at readableAddChunk (_stream_readable.js:176:18)
       at Socket.Readable.push (_stream_readable.js:134:10)
       at TCP.onread (net.js:548:20)
     code: 'ER_NO_SUCH_TABLE',
     errno: 1146,
     sqlMessage: 'Table \'shop.undefined\' doesn\'t exist',
     sqlState: '42S02',
     index: 0,
     sql: 'DROP INDEX `user` ON `undefined`',
     model: 'user' },
  isOperational: true,
  code: 'ER_NO_SUCH_TABLE',
  errno: 1146,
  sqlMessage: 'Table \'shop.undefined\' doesn\'t exist',
  sqlState: '42S02',
  index: 0,
  sql: 'DROP INDEX `user` ON `undefined`',
  model: 'user' }

Deeper investigation have led me through node-orm2 sources into this package and file https://github.com/dresende/node-sql-ddl-sync/blob/master/lib/Dialects/mysql.js#L294, to the function convertIndexRows. It gets an sql query result as the only parameter and tries to convert it into hash containing info about indexes existing on a table. But as I noticed, it deals with wrong column names in result.

Literally, it queries SELECT index_name, column_name, non_unique FROM information_schema.statistics WHERE table_schema = ? AND table_name = ? with params ["shop","user"] (aka [driver.config.database, name]). Typing the query by hand into mysql promt given this output:

mysql> SELECT index_name, column_name, non_unique FROM information_schema.statistics WHERE table_schema = 'shop' AND table_name = 'user';
+------------+-------------+------------+
| INDEX_NAME | COLUMN_NAME | NON_UNIQUE |
+------------+-------------+------------+
| email      | email       | 0          |
| PRIMARY    | id          | 0          |
+------------+-------------+------------+
2 rows in set (0.00 sec)

As you can see, here are column names in upper case, while in the function convertIndexRows you are indexing them in lower case like rows[i].index_name. Correcting every occurrence of index_name, column_name and non_unique inside this function to their capitalized version solved my issue. Is this a typo or I configured something wrong on my machine?

As a result of original version of function, I got this:

{ undefined: { columns: [ undefined ], unique: false } }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions