简体   繁体   English

使用带有 Sequelize ORM 和文字的原始 SQL 查询

[英]Using a raw SQL query with Sequelize ORM and literal

Using the Sequelize ORM I am trying to update the field level_id where this field has a foreign key to the field Level in another table called level_tbl.使用 Sequelize ORM,我尝试更新字段 level_id,其中该字段具有指向另一个名为 level_tbl 的表中的字段 Level 的外键。

 select * from level_tbl;
+----------+----------+
| level_id | Level    |
+----------+----------+
|        1 | Higher   |
|        2 | Ordinary |
+----------+----------+

My update task looks like this, and as you can see I am trying to get a raw sql query to work as a literal with Sequelize.我的更新任务看起来像这样,正如你所看到的,我试图让一个原始的 sql 查询作为一个文本与 Sequelize 一起工作。

  //Update task
    router.put("/task/:id", (req, res) => {
      if (!req.body) {
        res.status(400)
        res.json({
          error: "Bad Data....!"
        })
      } else {
        Task.update({
            Level: req.body.Level,
            Level_id: [sequelize.literal("SELECT level_id FROM level_tbl WHERE Level = 'Ordinary'")],
            Year: req.body.Year,
            Question: req.body.Question,
            Answer: req.body.Answer,
            Topic: req.body.Topic,
            Sub_topic: req.body.Sub_topic,
            Question_type: req.body.Question_type,
            Marks: req.body.Marks,
            Question_number: req.body.Question_number,
            Part: req.body.Part,
            Sub_part: req.body.Sub_part
          }, {
            where: {
              id: req.params.id
            }
          })
          .then(() => {
            res.send("Task Updated")
          })
          .error(err => res.send(err))
      }
    })

What would be the correct syntax for this line?这一行的正确语法是什么?

   Level_id: [sequelize.literal("SELECT level_id FROM level_tbl WHERE Level = 'Ordinary'")],

The issue is that I already have imported a model and have access to the global Sequelize instance.问题是我已经导入了一个模型并且可以访问全局 Sequelize 实例。 Therefore example in the documentation don't apply this way, ie,因此文档中的示例不适用于这种方式,即,

 order: sequelize.literal('max(age) DESC')

From https://sequelize.org/master/manual/querying.html来自https://sequelize.org/master/manual/querying.html

and also,并且,

https://github.com/sequelize/sequelize/issues/9410#issuecomment-387141567 https://github.com/sequelize/sequelize/issues/9410#issuecomment-387141567

My Task.js where the model is defined is as follows,我定义模型的 Task.js 如下,

const Sequelize = require("sequelize")
const db = require("../database/db.js")

module.exports = db.sequelize.define(
  "physics_tbls", {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    Level: {
      type: Sequelize.STRING
    },
    Level_id: {
      type: Sequelize.INTEGER
    },
    Year: {
      type: Sequelize.INTEGER
    },
    .........
  }, {
    timestamps: false
  }
)

I am using a MEVN stack -> MySQL, Express.js, Vue.js and Node.js我正在使用 MEVN 堆栈 -> MySQL、Express.js、Vue.js 和 Node.js

Any help would be greatly appreciated,任何帮助将不胜感激,

Thanks,谢谢,

I needed to require Sequelize again in tasks.js, the file the defines the express routes.我需要在 tasks.js 中再次要求 Sequelize,该文件定义了快速路由。 It wasn't enough just to require Task.js although Task.js does itself require sequelize.尽管 Task.js 本身需要 sequelize,但仅仅需要 Task.js 是不够的。

const Sequelize = require('sequelize')
var express = require("express")
var router = express.Router()
const Task = require("../model/Task")

Also brackets needed around the query and inside the double quotes,查询周围和双引号内还需要括号,

Level_id: Sequelize.literal("(SELECT level_id FROM level_tbl WHERE Level = 'Higher')"),

i'm using sequelize 6.3 and raw query on where is no longer supported, i'm using this syntax :我正在使用 sequelize 6.3 并且不再支持 where 上的原始查询,我正在使用以下语法:

where: sequelize.where(sequelize.col("table.column"), "=", "yourvalue")

and it worked它起作用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM