Sequelize Alter 始终将所有数据 table 外键设置为 null
Sequelize Alter always set all data table foreign key to null
错误描述
在 运行 sequelize 与选项 alter: true
同步后,table 中的所有外键都设置为 null
。
await sequelize.sync({ alter: true }).then(async (result) => {
await initDataUserGroup();
await initDataUserAdmin();
await initDataGateType();
});
这是我的一个模型,它有外键:
const { DataTypes } = require("sequelize");
const { sequelize } = require("../services/DatabaseService");
const UserGroup = require("./UserGroup");
const Biodata = require("./Biodata");
const User = sequelize.define("user", {
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
notNull: {
msg: "Username must not be empty",
},
notEmpty: true,
},
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
notNull: true,
notEmpty: true,
isEmail: true,
},
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: true,
notEmpty: true,
},
},
});
User.UserGroup = User.belongsTo(UserGroup);
User.Biodata = User.hasOne(Biodata, {
foreignKey: "userId",
as: "biodata",
});
module.exports = User;
你预计会发生什么?
在 运行 与选项 { alter: true }
同步后,不应将所有外键设置为空
实际发生了什么?
userGroupId
in table user
为空,userId
in table biodata
也为空。
环境
- 续集版本:^6.6.5
- Node.js版本:12.9.1
- 方言:SQLite (SQLite3)
来自我的问题:https://github.com/sequelize/sequelize/issues/13464
有人可以帮助我吗?
经过几天的挣扎,我设法通过在同步前后禁用并重新启用外键来解决它:
await sequelize.query("PRAGMA foreign_keys = false;");
await sequelize
.sync({ alter: true })
.then(async (result) => {
await initDataUserGroup();
await initDataGateType();
})
.catch((errorMessage) => console.error("sync error = ", errorMessage));
await sequelize.query("PRAGMA foreign_keys = true;");
请随时告诉我比当前更好的解决方案。
错误描述
在 运行 sequelize 与选项 alter: true
同步后,table 中的所有外键都设置为 null
。
await sequelize.sync({ alter: true }).then(async (result) => {
await initDataUserGroup();
await initDataUserAdmin();
await initDataGateType();
});
这是我的一个模型,它有外键:
const { DataTypes } = require("sequelize");
const { sequelize } = require("../services/DatabaseService");
const UserGroup = require("./UserGroup");
const Biodata = require("./Biodata");
const User = sequelize.define("user", {
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
notNull: {
msg: "Username must not be empty",
},
notEmpty: true,
},
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
notNull: true,
notEmpty: true,
isEmail: true,
},
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: true,
notEmpty: true,
},
},
});
User.UserGroup = User.belongsTo(UserGroup);
User.Biodata = User.hasOne(Biodata, {
foreignKey: "userId",
as: "biodata",
});
module.exports = User;
你预计会发生什么?
在 运行 与选项 { alter: true }
实际发生了什么?
userGroupId
in table user
为空,userId
in table biodata
也为空。
环境
- 续集版本:^6.6.5
- Node.js版本:12.9.1
- 方言:SQLite (SQLite3)
来自我的问题:https://github.com/sequelize/sequelize/issues/13464
有人可以帮助我吗?
经过几天的挣扎,我设法通过在同步前后禁用并重新启用外键来解决它:
await sequelize.query("PRAGMA foreign_keys = false;");
await sequelize
.sync({ alter: true })
.then(async (result) => {
await initDataUserGroup();
await initDataGateType();
})
.catch((errorMessage) => console.error("sync error = ", errorMessage));
await sequelize.query("PRAGMA foreign_keys = true;");
请随时告诉我比当前更好的解决方案。