如何使用 sequelize beforeCreate 来自动生成 Id 值?

问题描述 投票:0回答:1

我想通过使用sequelize beforeCreate来自动生成一个 BINARY(16) 值的模型,Id。

我使用了defaultValue参数,但最终得到的是

mysql中的重复键错误

.

所以,我发现,如果我使用beforeCreate,那么它将是uniqe每次,但当我做实际的创建IM得到了

Id不能为空的错误

我的模型。

const utility = require('utils/utilities');


module.exports = function(sequelize, DataTypes) {
    const weddings = sequelize.define(
        'weddings', {
            Id: {
                primaryKey: true,
                allowNull: false,
                type: 'BINARY(16)',
            },
            Name: {
                type: DataTypes.STRING(150),
                allowNull: false,
                comment: 'null',
            },
            HouseId: {
                type: DataTypes.INTEGER(11),
                allowNull: false,
                comment: 'null',
                references: {
                    model: 'house',
                    key: 'Id',
                },
            },
            WDate: {
                type: DataTypes.DATE,
                allowNull: false,
                comment: 'null',
            },
            Active: {
                type: DataTypes.BOOLEAN,
                allowNull: false,
                comment: 'null',
            },
        },

        {
            hooks: {
                beforeCreate() {
                const generateValue = Buffer.from(utility.generateUID().replace('-', ''), 'hex');
                weddings.Id = generateValue;
                }
            }
        }, {
            tableName: 'weddings',
        }
    );

    return weddings;
};

错误:

weddings.Id不能为空

我缺少什么?

javascript node.js sequelize.js
1个回答
1
投票

你可以使用像这样的实例钩子。

const utility = require('utils/utilities');


module.exports = function(sequelize, DataTypes) {
    const weddings = sequelize.define(
        'weddings', {
            Id: {
                primaryKey: true,
                allowNull: false,
                type: 'BINARY(16)',
            },
            Name: {
                type: DataTypes.STRING(150),
                allowNull: false,
                comment: 'null',
            },
            HouseId: {
                type: DataTypes.INTEGER(11),
                allowNull: false,
                comment: 'null',
                references: {
                    model: 'house',
                    key: 'Id',
                },
            },
            WDate: {
                type: DataTypes.DATE,
                allowNull: false,
                comment: 'null',
            },
            Active: {
                type: DataTypes.BOOLEAN,
                allowNull: false,
                comment: 'null',
            },
        },

        {
        }, {
            tableName: 'weddings',
        }
    );

    weddings.beforeCreate(async (data, options) => {
        data.Id = await Buffer.from(utility.generateUID().replace('-', ''), 'hex');

    });

    return weddings;
};

如果你想为每个单独的记录发出钩子,随着批量钩子,你可以通过: individualHooks: true 到调用。

table.update( req.body, {
  where: where,
  returning: true,
  individualHooks: true
  plain: true
})

创建。

  db.weddings.create({
      ...args,
    }),

其他信息你可以阅读它:https:/sequelize.orgv5manualhooks.html。

© www.soinside.com 2019 - 2024. All rights reserved.