sequelize-auto-migrations icon indicating copy to clipboard operation
sequelize-auto-migrations copied to clipboard

Postgres ARRAY types not generated correctly

Open dylanlingelbach opened this issue 8 years ago • 2 comments

We use ARRAY types and are testing out using sequelize-auto-migrations. ARRAY types don't generate correctly.

Minimal repro below:

  1. Define a new schema that uses an ARRAY type
'use strict';

const Sequelize = require('sequelize');

module.exports = function (sequelize) {
    const Test = sequelize.define('test', {
        array: {
            type: Sequelize.ARRAY(Sequelize.TEXT),
            allowNull: false,
            defaultValue: []
        }
    }, {
    });

    return Test;
};
  1. Generate the migration

Expected: Syntactically valid migration generated

'use strict';

var Sequelize = require('sequelize');

/**
 * Actions summary:
 *
 * createTable "tests", deps: []
 *
 **/

var info = {
    "revision": 2,
    "name": "testSchema",
    "created": "2017-08-02T16:42:10.042Z",
    "comment": ""
};

var migrationCommands = [{
        fn: "createTable",
        params: [
            "tests",
            {
                "id": {
                    "type": Sequelize.INTEGER,
                    "autoIncrement": true,
                    "primaryKey": true,
                    "allowNull": false
                },
                "array": {
                    "type": Sequelize.ARRAY(Sequelize.TEXT), // This line is wrong in the generated migration
                    "defaultValue": [],
                    "allowNull": false
                },
                "createdAt": {
                    "type": Sequelize.DATE,
                    "allowNull": false
                },
                "updatedAt": {
                    "type": Sequelize.DATE,
                    "allowNull": false
                }
            },
            {}
        ]
    }
];

module.exports = {
    pos: 0,
    up: function(queryInterface, Sequelize)
    {
        var index = this.pos;
        return new Promise(function(resolve, reject) {
            function next() {
                if (index < migrationCommands.length)
                {
                    let command = migrationCommands[index];
                    console.log("[#"+index+"] execute: " + command.fn);
                    index++;
                    queryInterface[command.fn].apply(queryInterface, command.params).then(next, reject);
                }
                else
                    resolve();
            }
            next();
        });
    },
    info: info
};

Actual:

'use strict';

var Sequelize = require('sequelize');

/**
 * Actions summary:
 *
 * createTable "tests", deps: []
 *
 **/

var info = {
    "revision": 2,
    "name": "testSchema",
    "created": "2017-08-02T16:42:10.042Z",
    "comment": ""
};

var migrationCommands = [{
        fn: "createTable",
        params: [
            "tests",
            {
                "id": {
                    "type": Sequelize.INTEGER,
                    "autoIncrement": true,
                    "primaryKey": true,
                    "allowNull": false
                },
                "array": {
                    "type": TEXT[], // This syntax is incorrect
                    "defaultValue": [],
                    "allowNull": false
                },
                "createdAt": {
                    "type": Sequelize.DATE,
                    "allowNull": false
                },
                "updatedAt": {
                    "type": Sequelize.DATE,
                    "allowNull": false
                }
            },
            {}
        ]
    }
];

module.exports = {
    pos: 0,
    up: function(queryInterface, Sequelize)
    {
        var index = this.pos;
        return new Promise(function(resolve, reject) {
            function next() {
                if (index < migrationCommands.length)
                {
                    let command = migrationCommands[index];
                    console.log("[#"+index+"] execute: " + command.fn);
                    index++;
                    queryInterface[command.fn].apply(queryInterface, command.params).then(next, reject);
                }
                else
                    resolve();
            }
            next();
        });
    },
    info: info
};

Thank you so much for this package. It's been a big help so far.

dylanlingelbach avatar Aug 02 '17 16:08 dylanlingelbach

This is a great library. But a bug with the type of ARRAY does not allow it to use with PostgreSQL. Are there any plans to fix this?

apapacy avatar Dec 31 '18 20:12 apapacy

Up

but1head avatar Mar 21 '19 08:03 but1head