Reputation: 27
I'm getting to grips with KeystoneJS and trying to display the many-to-many relationships in the AdminUI.
I have two models, playlist and trailer. They have a many-to-many relationship, defined as follows:
models/Playlist.js
Playlist.add('Playlist', {
name: { type: Types.Text, initial: true, required: true, index: true, unique: true },
description: { type: Types.Textarea, initial: true, required: true },
}
);
Playlist.relationship({ ref: 'Trailer', refPath: 'title', path: 'trailers' });
models/Trailer.js
Trailer.add('Trailer', {
title: { type: Types.Text, required: true, initial: true, index: true },
playlists: { type: Types.Relationship, ref: 'Playlist', many: true },
}
)
The relationship is modelled correctly in MongoDB; there is an array of trailer ObjectID's in the playlist's trailers field. But when I view the Playlist in the AdminUI, I just see "No related trailers..."
Why doesn't this work? I want to see the relationship in the AdminUI.
Documentation around this feature is useless. Just random code snippets without any context. I've also followed the example in Practical Keystone JS without any luck.
Upvotes: 1
Views: 684
Reputation: 12071
Regarding the relationship options:
path is the name you want for the field on the model. In this case, we might call this 'trailers**.
ref is the referred Model (the one that has the relationship field). In this case, 'Trailer'.
refPath is the field in the referenced model that has the relationship. In this case 'playlists'.
Try this:
// models/Playlist.js
var Playlist = new keystone.List('Playlist');
Playlist.add({
name: { type: Types.Text, initial: true, required: true, index: true, unique: true },
description: { type: Types.Textarea, initial: true, required: true },
});
Playlist.relationship({ path: 'trailers', ref: 'Trailer', refPath: 'playlists' });
// models/Trailer.js
var Trailer = new keystone.List('Trailer');
Trailer.add({
title: { type: Types.Text, required: true, initial: true, index: true },
playlists: { type: Types.Relationship, ref: 'Playlist', many: true },
});
Upvotes: 2