sequelize / cli

The Sequelize CLI
MIT License
2.53k stars 525 forks source link

changeColumn's primaryKey: Cannot read property '0' of undefined #529

Closed axetroy closed 7 years ago

axetroy commented 7 years ago

Model

import * as Sequelize from 'sequelize';
import sequelize from '../index';

const TradeStatModel = sequelize.define('tradeStat', {
  id: {
    primaryKey: true,
    type: Sequelize.UUID,
    required: true,
    defaultValue: Sequelize.UUIDV4,
    unique: true,
    allowNull: false
  },
  date: {
    type: Sequelize.DATE,
    required: true,
    allowNull: false,
    comment: '统计日期'
  },
  currency: {
    type: Sequelize.STRING,
    required: true,
    allowNull: false,
    comment: '交易的货币,默认base'
  },
  open: {
    type: Sequelize.FLOAT,
    required: true,
    allowNull: false,
    comment: '开盘价',
    validate: {
      min: 0
    }
  },
  close: {
    type: Sequelize.FLOAT,
    required: true,
    allowNull: false,
    comment: '收盘价',
    validate: {
      min: 0
    }
  },
  highest: {
    type: Sequelize.FLOAT,
    required: true,
    allowNull: false,
    comment: '最高价',
    validate: {
      min: 0
    }
  },
  lowest: {
    type: Sequelize.FLOAT,
    required: true,
    allowNull: false,
    comment: '最低价',
    validate: {
      min: 0
    }
  },
  amount: {
    type: Sequelize.FLOAT,
    required: true,
    allowNull: false,
    comment: '成交量',
    validate: {
      min: 0
    }
  },
  value: {
    type: Sequelize.FLOAT,
    required: true,
    allowNull: false,
    comment: '交易额 = 成交量 * 成交价',
    validate: {
      min: 0
    }
  },
  note: {
    type: Sequelize.STRING,
    required: false,
    allowNull: true,
    defaultValue: ''
  },
  active: {
    type: Sequelize.BOOLEAN,
    defaultValue: true,
    required: false,
    allowNull: true
  }
});

export default TradeStatModel;

Migration file

'use strict';

module.exports = {
  up: function(queryInterface, Sequelize) {
    /*
      Add altering commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.createTable('users', { id: Sequelize.INTEGER });
    */
    queryInterface.changeColumn('tradeStats', 'id', {
      type: Sequelize.UUID,
      required: true,
      defaultValue: Sequelize.UUIDV4,
      allowNull: false
    });
    queryInterface.changeColumn('tradeStats', 'date', {
      type: Sequelize.DATE,
      primaryKey: true,
      unique: true,
      required: true,
      allowNull: false,
      comment: '统计日期'
    });
  },

  down: function(queryInterface, Sequelize) {
    /*
      Add reverting commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.dropTable('users');
    */
    queryInterface.changeColumn('tradeStats', 'id', {
      primaryKey: true,
      type: Sequelize.UUID,
      required: true,
      defaultValue: Sequelize.UUIDV4,
      unique: true,
      allowNull: false
    });
    queryInterface.changeColumn('tradeStats', 'date', {
      type: Sequelize.DATE,
      required: true,
      allowNull: false,
      comment: '统计日期'
    });
  }
};

Stdout

Sequelize [Node: 6.11.0, CLI: 2.8.0, ORM: 4.7.5]

Loaded configuration file "config/config.json".
Using environment "development".
== 20170830041648-tradeStat-date-migration: migrating =======
Unhandled rejection TypeError: Cannot read property '0' of undefined
    at query.catch.then.then.queryResult (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/sequelize/lib/dialects/postgres/query.js:112:17)
    at tryCatcher (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
Unhandled rejection SequelizeDatabaseError: relation "date_unique_idx" already exists
    at Query.formatError (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/sequelize/lib/dialects/postgres/query.js:351:16)
    at query.catch.err (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/sequelize/lib/dialects/postgres/query.js:86:18)
    at tryCatcher (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/promise.js:689:18)
    at Async._drainQueue (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/home/axetroy/gpm/github.com/axetroy/duomi-nodejs/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
== 20170830041648-tradeStat-date-migration: migrated (0.022s)
sushantdhiman commented 7 years ago

Sequelize doesn't support pg@7.x use pg@6.x for now

axetroy commented 7 years ago

@sushantdhiman Thanks, I will try agian.

Can you specify the dependencies version in README.md?

$ npm install --save sequelize

# And one of the following:
$ npm install --save pg pg-hstore # required pg@6.x.x, @pg-hstore@2.x.x
$ npm install --save mysql2
$ npm install --save sqlite3
$ npm install --save tedious // MSSQL
ajbeach2 commented 6 years ago

this is still an issue in postgres 10