JavaScript-无法使用sequelize .create函数创建表

时间:2018-11-18 22:13:21

标签: javascript node.js sequelize.js

在文件顶部,我有const Sequelize = require('sequelize')。此后,我做了sequelize.authenticate(),并且可以连接数据库了。然后我用它来定义我的部门表:

var Department = sequelize.define('Department', {
    departmentId: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    departmentName: Sequelize.STRING,
    }, {
        createdAt: false,
        updatedAt: false
})

然后,我有sequelize.sync()可用。但是当我尝试创建表时:

module.exports.addDepartment = function(departmentData)
{
    return new Promise(function(resolve, reject){

        for (var i in departmentData)
        {
            if (departmentData[i] == "")
                departmentData[i] = null
        }

        Department.create(
            {
                departmentId: departmentData.departmentId,
                departmentName: departmentData.departmentName
            }
        ).then(() => { 
            resolve() 
        }).catch(() => { 
            reject("unable to create department") 
        })
    })
}

它不起作用。我确保departmentData.departmentName有一个值,并且departmentData.departmentId为空。无论我尝试什么,这都是它在控制台上显示的内容:

  

执行(默认):如果不存在“部门”(“ departments”(“ departmentId” SERIAL,“ departmentName” VARCHAR(255),PRIMARY KEY(“ departmentId”)),则创建表;   执行(默认):SELECT i.relname AS名称,ix.indisprimary AS主服务器,ix.indisunique AS唯一,ix.indkey AS indkey,array_agg(a.attnum)作为   column_indexes,array_agg(a.attname)AS column_names,pg_get_indexdef(ix.indexrelid)AS定义来自pg_class t,pg_class i,pg_index ix,pg_attribute WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a。 attrelid = t.oid AND t.relkind ='r'和t.relname ='部门'GROUP BY   i.relname,ix.indexrelid,ix.indisprimary,ix.indisunique,ix.indkey ORDER BY i.relname;   正在执行(默认):INSERT INTO“ Departments”(“ departmentId”,“ departmentName”)值(DEFAULT,'Creative Services')返回*;   无法创建部门

我在做什么错?有人对我可能做错了什么建议吗?

0 个答案:

没有答案