【问题标题】:sequelize Bulk insert fails when seedingsequelize 播种时批量插入失败
【发布时间】:2021-08-20 16:12:09
【问题描述】:

我在节点项目中使用 sequelize 作为 ORM 并尝试插入一些种子数据,但是失败了。

我正在尝试找到一种方法,以便由 sequelize 生成的插入语句忽略 Id 或接受我正在设置的 Id,并且在插入语句将 SET IDENTITY_INSERT 设置为 ON 之前,然后在插入集之后将其关闭。

我知道设置 needIdentityInsertWrapper:true 是后者,但我的语法似乎有问题。

下面是模型

module.exports = (sequelize, DataTypes) => {
    const Client = sequelize.define("Client", {
        Id: {
            primaryKey: true,
            type: "INTEGER IDENTITY(1,1)",
        },

        Name:{
            type: "VARCHAR(250)",
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },

        AddressLine1:{
            type: "VARCHAR(500)",
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },

        AddressLine2:{
            type: "VARCHAR(500)",
            allowNull: true,
        },

        AddressLine3:{
            type: "VARCHAR(500)",
            allowNull: true,
        },

        Postcode:{
            type: "VARCHAR(10)",
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },

        City:{
            type: "VARCHAR(100)",
            allowNull: true,
        },

        
        County:{
            type: "VARCHAR(50)",
            allowNull: true,
        },


        Country:{
            type: "VARCHAR(100)",
            allowNull: true,
        },

        ContactNumber : {
            type: "VARCHAR(20)",
            allowNull: true,
        },


        Email : {
            type: "VARCHAR(500)",
            allowNull: true,
        },

        CreatedAt :{
            type:"datetimeoffset(7) DEFAULT GETDATE()",
            allowNull: false
        },

        UpdatedAt :{
            type:"datetimeoffset(7)",
            allowNull: true
        }

    },
    {freezeTableName: true,  createdAt: false,updatedAt: false}
    );

    Client.associate=models=>{
        Client.hasMany(models.Invoice,{foreignKey:"ClientId"})
    }

   return Client;
}

这里是批量插入代码

var db = require('../models')

module.exports = async function () {
    return await db.Client.bulkCreate(
        [{
           // Id:1,
            name:"Company",
            AddressLine1:"Add 1",
            Postcode:"Postcode",
            City:"UK"
        }],{},
        {
            autoincrement :true,
            needIdentityInsertWrapper:true
        }
    )
}

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    为了解决这个问题,我做了以下代码更改

    module.exports = (sequelize, DataTypes) => {
    const Client = sequelize.define("Client", {
        Id: {
            primaryKey: true,
            type: "INTEGER",
            autoIncrement:true
        },
    
        Name:{
            type: "VARCHAR(250)",
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },
    
        AddressLine1:{
            type: "VARCHAR(500)",
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },
    
        AddressLine2:{
            type: "VARCHAR(500)",
            allowNull: true,
        },
    
        AddressLine3:{
            type: "VARCHAR(500)",
            allowNull: true,
        },
    
        Postcode:{
            type: "VARCHAR(10)",
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },
    
        City:{
            type: "VARCHAR(100)",
            allowNull: true,
        },
    
        
        County:{
            type: "VARCHAR(50)",
            allowNull: true,
        },
    
    
        Country:{
            type: "VARCHAR(100)",
            allowNull: true,
        },
    
        ContactNumber : {
            type: "VARCHAR(20)",
            allowNull: true,
        },
    
    
        Email : {
            type: "VARCHAR(500)",
            allowNull: true,
        },
    
        CreatedAt :{
            type:"datetimeoffset(7) DEFAULT GETDATE()",
            allowNull: false
        },
    
        UpdatedAt :{
            type:"datetimeoffset(7)",
            allowNull: true
        }
    
    },
    {freezeTableName: true,  createdAt: false,updatedAt: false}
    );
    
    Client.associate=models=>{
        Client.hasMany(models.Invoice,{foreignKey:"ClientId"})
    }
    
    return Client;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-31
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多