Is possible insert two objects equals in two models?? I have created two models Playlist and Song, and I have created a song1 and two playlists: playlist1 and playlist2; and I want that playlist1 have song1 and playlist2 have the same song (song1).
是否可以在两个模型中插入两个对象?我创建了两个模型Playlist和Song,我创建了一个song1和两个播放列表:playlist1和playlist2;我希望playlist1有song1和playlist2有相同的歌曲(song1)。
But when I relate the song1 in playlist1 and I relate song1 in playlist2, the relation that is in playlist1 dissappears. The relation is 1 to N, and I've tryed with a relation N to N, but I can't solve it.
但是当我将playlist1中的song1与播放列表2中的song1相关联时,播放列表1中的关系就会消失。关系是1到N,我尝试了N到N的关系,但我无法解决它。
Could anybody help me, please?? :(
请问有人帮帮我吗? :(
Sorry, my problem is that in a controller I have the main model and work with two models.
对不起,我的问题是在控制器中我有主模型并使用两个模型。
Main model:
--- Playlist.js ---
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
unique: true
},
// Associations
tablets_playlists: {
collection: 'Tablet',
via: 'lista_tablet'
},
songs_playlist: {
collection: 'Song',
via: 'song'
}
}
};
Other models:
--- Song.js ---
module.exports = {
attributes: {
group: {
type: 'string',
required: true,
unique: true
},
song: {
type: 'string'
},
// Association
song: {
collection: 'Playlist',
via: 'songs_playlist'
}
}
};
--- Tablet.js ---
module.exports = {
attributes: {
MAC: {
type: 'string',
required: true,
unique: true
},
// Associations
lista_tablet: {
collection: 'Playlist',
via: 'tablets_playlists'
}
};
And when I associate a song with playlist or a tablet with playlist in my render view don't appear. But if the relation is 1 to N, it appears. And when I associate two times the same song, the server crash, I think that is because there are or its trying add two equals objects in the bbdd.
当我在播放视图中将歌曲与播放列表或平板电脑与播放列表相关联时,不会出现。但如果关系是1到N,则会出现。当我将同一首歌关联两次,服务器崩溃时,我认为这是因为有或者它试图在bbdd中添加两个等于对象。
This is my method to associate a tablet with a playlist and similar to song (I posted before).
这是我将平板电脑与播放列表相关联的方法,类似于歌曲(我之前发布过)。
associateTabletToPlaylist: function(req,res,next){
var tablet_id = req.param('tablet_id');
var playlist_id = req.param('playlist_id');
Playlist.findOne(playlist_id, function foundPlaylist(err, playlist) {
if (err) return next(err);
if (!playlist) return next();
playlist.tablets_playlists.add(tablet_id);
playlist.save(function createAssociation(err, saved) {
if (err) res.redirect('/playlist/associateTablet/');
res.redirect('/playlist/show/' + playlist_id);
});
});
}
And this is the code in PlaylistController to show, to render the view:
这是PlaylistController中要显示的代码,用于呈现视图:
// render the profile view (e.g. /playlist/show/.ejs)
show: function(req, res, next) {
Playlist.findOne(req.param('id'), function foundPlaylist(err, playlist) {
if (err) return next(err);
if (!playlist) return next();
Tablet.find({'listas_reproduccion':playlist.id},function foundTablets(err, tablets) {
if (err) return next(err);
if (!tablets) return next();
Song.find({'song':playlist.id}, function foundSongs(err, songs) {
if (err) return next(err);
if (!songs) return next();
res.view({
songs: songs,
tablets: tablets,
playlist: playlist
});
});
});
});
},
1 个解决方案
#1
2
Looks like you have one-to-one relations, but you need many-to-many.
看起来你有一对一的关系,但你需要多对多。
Song have many Playlists, and Playlist have many Songs. You must give via attribute:
歌曲有很多播放列表,播放列表有很多歌曲。你必须通过属性给出:
Songs model:
module.exports = {
attributes: {
name:'STRING',
// Add a reference to Playlists
playlists: {
collection: 'playlists',
via: 'songs'
}
}
}
Playlists model:
module.exports = {
attributes: {
name:'STRING',
// Add a reference to Songs
songs: {
collection: 'songs',
via: 'playlists'
}
}
}
#1
2
Looks like you have one-to-one relations, but you need many-to-many.
看起来你有一对一的关系,但你需要多对多。
Song have many Playlists, and Playlist have many Songs. You must give via attribute:
歌曲有很多播放列表,播放列表有很多歌曲。你必须通过属性给出:
Songs model:
module.exports = {
attributes: {
name:'STRING',
// Add a reference to Playlists
playlists: {
collection: 'playlists',
via: 'songs'
}
}
}
Playlists model:
module.exports = {
attributes: {
name:'STRING',
// Add a reference to Songs
songs: {
collection: 'songs',
via: 'playlists'
}
}
}