简体   繁体   English

在NodeJS和MySQL中为标记系统进行3表更新的最佳方法

[英]Best Approach to a 3 Table Update for Tagging System in NodeJS and MySQL

I'm writing an application that let's a user post content (called "moments") along with one or more tags associated with that content. 我正在编写一个应用程序,让用户发布内容(称为“时刻”)以及与该内容关联的一个或多个标签。

I have a 3 table data model (in MySQL) that looks like this: 我有一个3表数据模型(在MySQL中),如下所示:

  • Table: MOMENTS, Columns: MOMENT_ID, USER_ID, TITLE, CREATE_DATE 表格:MOMENTS,列:MOMENT_ID,USER_ID,TITLE,CREATE_DATE
  • Table: TAGS, Columns: TAG_ID, TITLE, CREATE_DATE 表格:TAGS,列:TAG_ID,TITLE,CREATE_DATE
  • Table: MOMENTS_TAGS, Columns: MOMENT_ID, TAG_ID 表格:MOMENTS_TAGS,列:MOMENT_ID,TAG_ID

Auto-Increment is set for: MOMENT_ID and TAG_ID 为以下内容设置了自动递增:MOMENT_ID和TAG_ID

The problem I'm trying to solve (in NodeJS) is: how do I insert a new post (in MOMENT table) and 1 or more tags (in TAGS table), get their respective ID's and do a 3rd insert into the junction table (MOMENT_TAGS)? 我要解决的问题(在NodeJS中)是:如何插入新帖子(在MOMENT表中)和1个或多个标签(在TAGS表中),获取它们各自的ID并在联结表中进行第3次插入(MOMENT_TAGS)?

I've already attempted to solve this (using mysqljs) by doing 3 successive queries, first MOMENTS, then TAGS, then MOMENTS_TAGS, but it fails because although the first result1.insertId returns the correct number, the 2nd result2.insertId doesn't seem to return the correct number from the TAGS table and instead simply increments the first result1.insertId. 我已经尝试通过执行3个连续的查询来解决此问题(使用mysqljs),先执行3个MOMENTS,然后是TAGS,然后是MOMENTS_TAGS,但是它失败了,因为尽管第一个result1.insertId返回正确的数字,但是第二个result2.insertId却没有似乎从TAGS表中返回了正确的数字,而是简单地增加了第一个result1.insertId。

Here is my code so far (I'm trying to get this to work for 1 tag at first): 到目前为止,这是我的代码(我试图首先将其用于1个标签):

app.post('/api/post/complex', function(req,res) {

  pool.getConnection(function(err, connection) {

    var preppedQuery1 = "INSERT INTO MOMENTS (USER_ID, TITLE, CREATE_DATE) VALUES ('justatest@gmail.com','test moment title','2016-08-08')";
    connection.query(preppedQuery1, function(err1, result1) { 

      if (err1) {
        res.send('ERROR');  
        connection.release();
      } 
      else {

        var preppedQuery2 = "INSERT INTO TAGS (TITLE, CREATE_DATE,USER_ID) VALUES ('TEST TAG', '2016-09-08','justatest@gmail.com')";
        connection.query(preppedQuery1, function(err2, result2) {  

        if (err2) {
          res.send('ERROR');  
          connection.release();
        } 
        else {

          var preppedQuery3 = "INSERT INTO MOMENTS_TAGS (MOMENT_ID, TAG_ID) VALUES (" + result1.insertId + "," + result2.insertId + ")";
          //At this point if result.insertId is 100, result2.insertId is always 101 instead of coming from the TAGS table
          //Why isn't result2.insertId the auto-incremented number from the TAGS table insert?

connection.query(preppedQuery3, function(err3, result3) {  

            if (err3) {         
              res.send('ERROR');
              connection.release();  
            } 
            else {
              console.log(result3); //not really needed
              res.send('OK'); //everything worked
              connection.release();

            }

          });

        }

        });

      } 

    });

  });

});

It looks like you're running preppedQuery1 twice. 看来您要运行两次preppedQuery1

Typo here: 错字在这里:

错字

(Assuming that's not just a typo in the post and actually exists in your source...) (假设这不仅是帖子中的错字,而且实际上还存在于您的来源中……)

Resolved: typo in 2nd preppedQuery (used 1 again, instead of 2). 解决:第二个preppedQuery中的错字(再次使用1,而不是2)。

Tested things and correct insertId is being assigned. 经过测试的东西和正确的insertId被分配。

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

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