当我在 table 中添加新列时,Sequelize 没有更新
Sequelize is not updated when I added new column in table
我无法解释为什么当我在 table 中添加新列时我的续集模型没有更新。
我在我的 table 中添加了新列 'status INTEGER' 并在我的 table 的模型中进行了如下更新。当我从 table 中检索值时,它什么也没有。
},
status: {
type: DataTypes.INTEGER,
default: 0
}
请帮我解决这个问题。甚至我 运行 迁移如下。
module.exports = {
up: function (migration, DataTypes, done) {
function addDisabledColumn() {
return migration.addColumn('applications', 'status',
{
type: DataTypes.INTEGER,
default: 0
}
)
}
addDisabledColumn().then(function () {
done();
}, function (err) {
done(err);
});
},
down: function (migration, DataTypes, done) {
done()
}
};
您需要重新同步 table 以使更改生效尝试 sequalize.sync({force:true})
希望对您有所帮助:)
When I retrieve value from my table, it coming nothing.
同样的事情发生在我身上。我创建了一个向 table 添加一列的迁移。当我使用 .findAll
、.findOne
、findAllAndCount
等时,该列将不存在。
您可以通过两种方式获取新列:
将新列添加到模型定义中。这不仅是获取带有 .find
查询的新列所必需的,而且还能够在调用 .create
时保存该列。如果这不起作用:
使用attributes
并显式定义要检索的新列:
const employee = await Employee.findOne({
where: { id: id },
attributes: ['id', 'name', 'NEW_COLUMN'],
});
如果您要在多个地方查询 table,最好为您要查询的字段创建一个别名,并在您 运行 的任何地方使用它查找查询:
const Employee = sequelize.define('employee', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
// DONT FORGET TO ADD THE NEW COLUMN TO YOUR MODEL
NEW_COLUMN: {
type: DataTypes.STRING,
allowNull: true,
}
// ...
});
Employee.basicAttributes = (alias = 'employee') => [
'id',
'name',
'NEW_COLUMN',
// ...
];
const employee = await Employee.findOne({
where: { id: id },
attributes: Employee.basicAttributes(),
});
它并不理想,但它确实有效,而且不需要 运行 sequalize.sync({force:true})
。
现在,如果您查看 API Reference 并滚动到 public async sync
方法。您将看到带有 drop
的 alter
选项,这意味着我们可以使用 sequalize.sync({alter:true, drop: false})
更新模型列而无需删除语句。
它应该更新 table 上缺失的列并且可以安全地用于生产。
我无法解释为什么当我在 table 中添加新列时我的续集模型没有更新。
我在我的 table 中添加了新列 'status INTEGER' 并在我的 table 的模型中进行了如下更新。当我从 table 中检索值时,它什么也没有。
},
status: {
type: DataTypes.INTEGER,
default: 0
}
请帮我解决这个问题。甚至我 运行 迁移如下。
module.exports = {
up: function (migration, DataTypes, done) {
function addDisabledColumn() {
return migration.addColumn('applications', 'status',
{
type: DataTypes.INTEGER,
default: 0
}
)
}
addDisabledColumn().then(function () {
done();
}, function (err) {
done(err);
});
},
down: function (migration, DataTypes, done) {
done()
}
};
您需要重新同步 table 以使更改生效尝试 sequalize.sync({force:true})
希望对您有所帮助:)
When I retrieve value from my table, it coming nothing.
同样的事情发生在我身上。我创建了一个向 table 添加一列的迁移。当我使用 .findAll
、.findOne
、findAllAndCount
等时,该列将不存在。
您可以通过两种方式获取新列:
将新列添加到模型定义中。这不仅是获取带有
.find
查询的新列所必需的,而且还能够在调用.create
时保存该列。如果这不起作用:使用
attributes
并显式定义要检索的新列:const employee = await Employee.findOne({ where: { id: id }, attributes: ['id', 'name', 'NEW_COLUMN'], });
如果您要在多个地方查询 table,最好为您要查询的字段创建一个别名,并在您 运行 的任何地方使用它查找查询:
const Employee = sequelize.define('employee', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, // DONT FORGET TO ADD THE NEW COLUMN TO YOUR MODEL NEW_COLUMN: { type: DataTypes.STRING, allowNull: true, } // ... }); Employee.basicAttributes = (alias = 'employee') => [ 'id', 'name', 'NEW_COLUMN', // ... ]; const employee = await Employee.findOne({ where: { id: id }, attributes: Employee.basicAttributes(), });
它并不理想,但它确实有效,而且不需要 运行 sequalize.sync({force:true})
。
现在,如果您查看 API Reference 并滚动到 public async sync
方法。您将看到带有 drop
的 alter
选项,这意味着我们可以使用 sequalize.sync({alter:true, drop: false})
更新模型列而无需删除语句。
它应该更新 table 上缺失的列并且可以安全地用于生产。