小编Myl*_*ith的帖子

如何使用sequelize v6以ES6语法关联模型

我在express/nuxt Web应用程序中使用sequelize ORM 6.4.0和CLI 6.2.0。(另外:节点 14.15.3、npm 6.14.11)。

背景:目标是制作一个 Web 应用程序,用于创建 mariokart 锦标赛并跟踪有关各个赛车手在不同赛道、车辆等上的表现的数据。问题是围绕 postgres/sequelize 关联以及正确创建/使用它们。

我正在尝试创建一个“赛车”模型,该模型具有来自车辆表的默认车辆。我在我的表中使用 postgres,并且使用 API 的express和sequelize创建表或与表交互时没有遇到任何问题。

这是我的 racer.js 模型:

'use strict'
const {
  Model
} = require('sequelize')
module.exports = (sequelize, DataTypes) => {
  class Racer extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate (models) {
      // define association here
      this.belongsTo(models.Vehicle, {
        foreignKey: 'vehicleName',
        as: …
Run Code Online (Sandbox Code Playgroud)

postgresql node.js sequelize.js sequelize-cli es6-class

5
推荐指数
1
解决办法
9331
查看次数