且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Sequelize 模型关联不会创建新列

更新时间:2021-09-10 03:27:41

感谢 Anatoly 像这样了解我关于 Sequelize 的问题.

Thanks to Anatoly for keeping up with my questions about Sequelize like this.

经过这么多次试验和错误,我认为问题是由我的路由注册引起的,例如:

After so many trials and errors, I figured that the issue was caused by the registration of my routes like:

require("./app/routes/user/user.routes")(app)

在我的 app.jsserver.js 中.这些路由注册是在 db.sync!

in my app.js or server.js. These routes registration was added before the db.sync!

所以我所做的是,在 db.sync 之后调用这些路由注册,就像这样:

So what I did was, I call these routes registration after that db.sync, like so:

const db = require("./app/models")

if (process.env.NODE_ENV === "production") {
  db.sequelize.sync().then(() => {
    useRoutes()
  })
} else {
  db.sequelize.sync({ force: true }).then(() => {
    console.log("Drop and re-sync db.")
    useRoutes()
  })
}

function useRoutes() {
  console.log("Use routes...")
  require("./app/routes/user/user.routes")(app)
  require("./app/routes/auth/auth.routes")(app)
}

瞧,固定!