0

How to use the 'via' attribute in a collection with more than one PK? Below is an example of a hasMany datamodel.

The Model Definition.

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'
        }
    }
});

The Collection Definition with the two 'via' attributes.

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' // ?????????
        }
    }
});
mluis
  • 213
  • 2
  • 13

1 Answers1

0

Do not use via like this, because value of via will be overwritten. So in your case will be like that

from_device: {
   model: 'device'
   //via: 'name', <-- omitted
   via: 'uuid'
}
SkyQ
  • 380
  • 2
  • 9
  • Ok I understood your point but the question remains. How to use both PKs? Thank you nonetheless. – mluis Oct 18 '16 at 07:53
  • I'm afraid that this is not possible. In DB can be only one PK. Other unique fields is treated like regular fileds – SkyQ Oct 18 '16 at 10:37
  • 1
    So if you really want to have two fields as the primary key must rebuild the structure of the DB and create two tables where one will be the first primary key, and the second will be another PK – SkyQ Oct 18 '16 at 10:44