Equivalent of { type: Schema.Types.ObjectId, ref:"myModel" } from mongoose in Sequelize. NodeJS

huangapple go评论129阅读模式
英文:

Equivalent of { type: Schema.Types.ObjectId, ref:"myModel" } from mongoose in Sequelize. NodeJS

问题

我需要使用Sequelize编写一个模型,但我找不到类似于Schema.Types.ObjectId, ref:"myModel"的等效项。我该如何实现这个?我对这项技术和数据库都很陌生。

我正在尝试从另一个模型中获取用户ID,并在创建DB实例时将其放入这个模型中。这将帮助我实现JWT令牌模型。

const User = require('../').user
const { Sequelize } = require('sequelize')

module.exports = function (sequelize) {
  const model = sequelize.define("TokenModel", {
    user: {
      type: //从另一个模型中获取的ID,类似于Mongoose中的方式
    },
    refreshToken: {
      type: Sequelize.STRING,
    },
  }, {
    timestamps: false
  })

  return model
}
英文:

I need to write a model with help of Sequelize, but I can't find an equivalent of Schema.Types.ObjectId, ref:"myModel" there. How I can implement this one? I'm new in this technology and in DataBases.

I'm trying to get an User Id from another model and put to this one when creating an instance in DB. It' ll help me to implement JWT token model.

const User = require('../').user
const {Sequelize} = require('sequelize')

module.exports = function (sequelize) {
  const model = sequelize.define("TokenModel", {
    user : {
      type: //Id from another Model like in Mongoose
    }
    refreshToken: {
      type: Sequelize.STRING,
    },
  }, {
    timestamps: false
  })

  return model
}

答案1

得分: 0

我明白了:

modelA = 属于(modelB);
modelA = 拥有一个(modelB);

问题已解决。抱歉,我对SQL数据库基础知识了解不够。

英文:

Okey, I figured out:

modelA = belongsTo(modelB);
modelA = hasOne(modelB);

Solved this. Sorry for my bad knowledge of basics of SQL databases Equivalent of { type: Schema.Types.ObjectId, ref:"myModel" } from mongoose in Sequelize. NodeJS

huangapple
  • 本文由 发表于 2023年8月10日 22:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定