【问题标题】:How to use the 'via' attribute in a collection with more than one PK?如何在具有多个 PK 的集合中使用“via”属性?
【发布时间】:2016-10-17 10:25:31
【问题描述】:

如何在具有多个 PK 的集合中使用 'via' 属性? 下面是一个 hasMany 数据模型的示例。

模型定义。

Persistence.prototype.collections.Device = Waterline.Collection.extend({
    identity: 'device',
    connection: 'default',
    attributes: {
        name: {
            type: 'string',
            required: true,
            primaryKey: true
        },
        uuid:{
            type: 'string',
            required: true,
            primaryKey: true
        },
        dataItems: {
            collection: 'dataitem',
            via: 'id'
        }
    }
});

具有两个“via”属性的集合定义。

Persistence.prototype.collections.DataItem = Waterline.Collection.extend({
    identity: 'dataitem',
    connection: 'default',
    attributes: {
        id: {
            type: 'string',
            unique: true,
            primaryKey: true
        },
        category: 'string',
        type: 'string',
        from_device: {
            model: 'device'
            via: 'name', // ??????
            via: 'uuid' // ?????????
        }
    }
});

【问题讨论】:

    标签: javascript sails.js waterline sails-postgresql


    【解决方案1】:

    不要像这样使用via,因为via的值会被覆盖。所以在你的情况下会是这样的

    from_device: {
       model: 'device'
       //via: 'name', <-- omitted
       via: 'uuid'
    }
    

    【讨论】:

    • 好的,我明白你的意思,但问题仍然存在。如何使用两个PK?不过还是谢谢你。
    • 恐怕这是不可能的。 DB中只能有一个PK。其他唯一字段被视为常规字段
    • 所以如果你真的想有两个字段作为主键必须重建数据库的结构并创建两个表,其中一个将是第一个主键,第二个将是另一个 PK跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多