0
votes

How to change values ​​in columns using only sequelize migration without clean requests? upsert or other?

Sequelize CLI [Node: 8.11.3, CLI: 4.0.0, ORM: 5.0.0-beta.5]

Loaded configuration file "config/database.json". Using environment "development". == 2018061713010-changeColumn: migrating =======

ERROR: Cannot read property 'uniqueKeys' of undefined

what it is error?

'use strict';

 module.exports = {
   up: (queryInterface, Sequelize) => {
    queryInterface.upsert(
      'table',
      {
        style: 2,
      },
      {
        style: 2,
      },
      {}, //model
      {} // options
    );
  },

  down: (queryInterface, Sequelize) => {

  },
};

enter image description here

database image

2
Please explain "without clean requests". And also, you should post code, not screenshots - simon.ro

2 Answers

3
votes

It seems I came up with a solution:

'use strict';
 const sequelize = require('..yourpath').sequelize; 
 module.exports = {
   up: (queryInterface, Sequelize) => {
    queryInterface.upsert(
      'table',
      {
        style: 2,
      },
      {
        style: 2,
      },
      sequelize.define('your Model', {
      id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false
    }, 
      {} // options
    );
  },

You have to create either a temporary Model or you can just use your shortcut then instead wherever you store your models. Hope this helps! Worked for me.

0
votes

I also was struggling with the same problem. It's the same approach you have used.

In case someone comes across this, here's the code I used.

const tableName = 'some_config_table';

module.exports = {
  up: (queryInterface, Sequelize) => {
    const { sequelize } = queryInterface;

    const model = sequelize.define(tableName, {
      key: {
        type: Sequelize.STRING,
        allowNull: false,
        unique: true,
      },
      value: Sequelize.STRING,
      createdAt: Sequelize.DATE,
      updatedAt: Sequelize.DATE,
    });

    return queryInterface.upsert(
      tableName,
      {
        key: 'my_config_key',
        value: 1,
        createdAt: new Date(),
        updatedAt: new Date(),
      },
      {
        value: 1,
      },
      {
        key: 'my_config_key',
      },
      model,
      {},
    );
  },

  down: (queryInterface, Sequelize) => {
    const { sequelize } = queryInterface;

    const model = sequelize.define(tableName, {
      key: {
        type: Sequelize.STRING,
        allowNull: false,
        unique: true,
      },
      value: Sequelize.STRING,
      createdAt: Sequelize.DATE,
      updatedAt: Sequelize.DATE,
    });

    return queryInterface.upsert(
      tableName,
      {
        key: 'my_config_key',
        value: 10,
        createdAt: new Date(),
        updatedAt: new Date(),
      },
      {
        value: 10,
      },
      {
        key: 'my_config_key',
      },
      model,
      {},
    );
  },
};