【问题标题】:Sails.js associationsSails.js 关联
【发布时间】:2015-06-07 15:40:38
【问题描述】:

我从sails.js 开始,我完全迷失了我的sql 查询。

我有以下表格:

genres
+-----------+--------------+------+-----+
| Field     | Type         | Null | Key |
+-----------+--------------+------+-----+
| id        | int(6)       | NO   | PRI | 
| name      | varchar(100) | NO   |     | 
| slug      | varchar(255) | NO   |     |
| type      | varchar(32)  | NO   |     | 
| parent_id | int(11)      | YES  | MUL | 
+-----------+--------------+------+-----+
genres_radios
+----------+--------+------+-----+
| Field    | Type   | Null | Key |
+----------+--------+------+-----+
| genre_id | int(6) | NO   | MUL |
| radio_id | int(6) | NO   | MUL |
+----------+--------+------+-----+
radios
+-----------+--------------+------+-----+
| Field     | Type         | Null | Key |
+-----------+--------------+------+-----+
| id        | int(5)       | NO   | PRI | 
| name      | varchar(100) | NO   |     | 
| slug      | varchar(100) | NO   |     |
| url       | varchar(100) | NO   |     | 
+-----------+--------------+------+-----+

我想检索收音机及其相关类型。我设法使用Model.query("Select * FROM ...") 来做到这一点,但我想使用填充方法来做到这一点。我查看了文档,但对"via""through"、...

【问题讨论】:

    标签: mysql node.js associations sails.js


    【解决方案1】:

    如果您遵循了Sails.js Model documentationmany-many association docs,您的模型应该如下所示:

    // api/models/genre.js
    module.exports = {
        attributes : {
            name : {
                type: 'string'
            },
            slug : {
                type: 'string'
            },
            type : {
                type: 'string'
            },
            radios : {
                collection: 'radio',
                via: 'genres'
            }
    
        }
    }
    
    // api/models/radio.js
    module.exports = {
        attributes : {
            name : {
                type: 'string'
            },
            slug : {
                type: 'string'
            },
            url : {
                type: 'string'
            },
            genres : {
                collection: 'genre',
                via: 'radios'
            }
    
        }
    }
    

    多对多查找表将由水线在内部为您创建。为您的收音机获取流派所需的只是填充“流派”属性。

    Radio.findOne({name:"RadioName"}).populate("genres").then(function(radio){
        console.log(radio); //radio.genres will have all the genres associated with this radio. 
    })
    

    我真的建议您查看many-many association docs。他们有你所需要的。

    【讨论】:

    • 我已经这样做了,但在我的结果中,流派属性为空。看看你写的内容,我不明白我如何能够检索电台的流派,因为它没有被告知它应该寻找哪个genre_id(radio_id 和genre_id 存储在genres_radios 表中)
    • 啊,这有点奇怪,因为您已经有一个现有的表。 Sails 实际上会为您创建genres_radios 查找表。
    【解决方案2】:

    应该这样做:

    // api/models/Genres.js
    module.exports = {
        attributes : {
            name : {
                type: 'string'
            },
            slug : {
                type: 'string'
            },
            type : {
                type: 'string'
            },
            radios : {
                collection: 'Radios',
                through: 'genres_radios'
            }
         }
    }
    
    // api/models/Radios.js
    module.exports = {
        attributes : {
            name : {
                type: 'string'
            },
            slug : {
                type: 'string'
            },
            url : {
                type: 'string'
            },
            genres : {
                collection: 'genre',
                through: 'genres_radios'
            }
        }
    }
    
    // api/models/Genres_radios.js
    module.exports = {
        attributes = {
            'Genre_id': {
                columnName:'genre_id',
                type:'integer',
                foreignKey:'true',
                references:'genres',
                on:'id',
                via:'genres'
            },
            'Radio_id': {
                columnName:'radio_id',
                type:'integer',
                foreignKey:'true',
                references:'radios',
                on:'id',
                via:'radios'
            }
        }
    }
    

    然后您可以提出以下请求:

    Radio.findOne({name:"RadioName"}).populate("genres").then(function(radio){
        console.log(radio); 
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 2014-05-03
      • 2014-10-24
      • 2015-06-02
      • 2014-06-20
      • 1970-01-01
      相关资源
      最近更新 更多