TypeError: s.replace is not a function
Asked Answered
B

1

5

Env:

  • Postgres: 10.2
  • Node: 6.11.0
  • CLI: 2.4.0
  • ORM: 2.1.3

Model:

'use strict';
module.exports = function(sequelize, DataTypes) {
    var test = sequelize.define('test', {
        id: DataTypes.UUID,
        type: DataTypes.STRING,
        data: DataTypes.JSON,
    }, {
        'createdAt': {
            type: Sequelize.DATE(3),
            defaultValue: Sequelize.literal('CURRENT_TIMESTAMP(3)'),
        },
        'updatedAt': {
            type: Sequelize.DATE(3),
            defaultValue: Sequelize.literal('CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)'),
        },
        classMethods: {
            associate: function(models) {
                // associations can be defined here
            }
        }
    });
    return test;
}

Migration:

'use strict';
module.exports = {
    up: function(queryInterface, Sequelize) {
        return queryInterface.addColumn('test', {
            test_col: {
                type: Sequelize.JSONB
            }
        });
    },
    down: function(queryInterface, Sequelize) {
        return queryInterface.removeColumn('test', 'test_col');
    }
};

Error:

TypeError: s.replace is not a function
    at Object.removeTicks (/node_modules/sequelize/lib/utils.js:329:14)
    at Object.addTicks (/node_modules/sequelize/lib/utils.js:325:29)
    at Object.quoteIdentifier (/node_modules/sequelize/lib/dialects/postgres/query-generator.js:835:22)
    at Object.addColumnQuery (/node_modules/sequelize/lib/dialects/postgres/query-generator.js:182:19)
    at QueryInterface.module.exports.QueryInterface.addColumn 
Barnacle answered 19/2, 2018 at 16:10 Comment(3)
Probably because s is nullTrepang
@AlexanderMP but y is s is null ? Any idea ?Barnacle
If I knew, I wouldn't have written it in a comment :)Trepang
K
12

The syntax used for migration is incorrect. The queryInterface.addcolumn method expects 3 arguments while you are passing only 2. See here.

public addColumn(table: String, key: String, attribute: Object, options: Object): Promise

'use strict';
module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.addColumn(
          'pages',
          'group',
          Sequelize.JSONB
      );
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.removeColumn('pages', 'group');
  }
};

And to anybody getting s.replace error it is basically because you are not passing the expected type of argument. So in above case, an object was passed as second parameter to queryInterface.addColumn while it was expecting a String literal. Read more here.

Kalmia answered 19/2, 2018 at 17:19 Comment(1)
Thank you for the note on s.replace. For me, I had a poorly formatted where:{} definition on a Model.findAll method call.Galvanoscope

© 2022 - 2024 — McMap. All rights reserved.