【发布时间】:2020-12-26 18:52:57
【问题描述】:
我使用迁移在 postgresql 数据库中的旧表中添加了一个新列,并为该列分配了默认值。
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.addColumn(
'notifications', // table name
'flag', // new field name
{
type: Sequelize.INTEGER,
defaultValue: 0,
allowNull: false,
},
),
]);
},
down: async (queryInterface, Sequelize) => {
}
};
现在我想更改为该列的特定值。我怎么能用钩子或服务做到这一点?我是 node.js 的新手,非常感谢您的帮助。
我的通知.model.js
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const notifications = sequelizeClient.define('notifications', {
senderId: {
type: DataTypes.INTEGER,
allowNull: false
},
recipientId: {
type: DataTypes.INTEGER,
allowNull: false
},
notificationtype: {
type: DataTypes.STRING,
allowNull: false
},
postId:{
type:DataTypes.INTEGER,
allowNull:true
},
conversationId:{
type:DataTypes.INTEGER,
allowNull:true
}
},
);
return notifications;
};
这是我的 notification.hooks.js。 我应该添加什么来将标志更新为 1?
const { authenticate } = require('@feathersjs/authentication').hooks;
const authHooks = require('feathers-authentication-hooks');
const { fastJoin, discard, disallow } = require('feathers-hooks-common');
const acquireNotification = require('../../hooks/notifications/acquire_notification');
const relateWithUser = require('../../hooks/notifications/relate_with_users');
const reverse = require('../../hooks/common/reverse');
module.exports = {
before: {
all: [
authenticate('jwt'),
reverse(),
],
find: [
acquireNotification(),
reverse()
],
get: [
acquireNotification()
],
create: [
authHooks.associateCurrentUser({
idField: 'id',
as: 'senderId'
}),
],
update: [
],
patch: [
],
remove: [
]
},
after: {
all: [],
find: [
fastJoin(relateWithUser),
],
get: [],
create: [
fastJoin(relateWithUser),
],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
【问题讨论】:
-
您希望更新一些记录并将这些记录的
flag字段值设置为指定值吗? -
是的,我正在尝试这样做:/
-
您是否使用 Sequelize 定义模型?如果是这样显示他们
-
我编辑我的问题以显示
标签: node.js postgresql migration sequelize.js