在sequilize.js中使用对象数组时出错了?

时间:2018-09-25 17:43:24

标签: sequelize.js

虽然我尝试编译此代码

const UserDetail = connection.define('user_detail', {
    id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        autoIncrement: true,
    },
    name: {
        type: Sequelize.STRING,
        allowNull: false,
        len: [6, 50],

    },
    about: {
        type: Sequelize.TEXT,
        allowNull: false,
    },
    position: {
        type: Sequelize.STRING,
        allowNull: false
    },
    birth: [{
        year: {
            type: Sequelize.INTEGER,
            allowNull: false
        },
        location: {
            type: Sequelize.TEXT,
            allowNull: false
        },
    }]
});

它抛出此错误

Unhandled rejection SequelizeDatabaseError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[object Object], `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, P' at line 1

创建对象数组的好方法是什么?我用谷歌搜索了这个问题,但找不到解决方案,而我在Stack Overflow上没有找到解决方案。

1 个答案:

答案 0 :(得分:0)

如果希望它成为“数组”,则可以使用其他相关表,也可以将字段定义为:

birth_year: {
  type: Sequelize.INTEGER,
  allowNull: false,
},
birth_location: {
  type: Sequelize.TEXT,
  allowNull: false,
},