我正在尝试将我的knex数据植入到我的postgres数据库中。但是,我注意到有时一个Promise.all([...])中的插入会插入到前一个或后一个Promise.all([..])表中。
exports.seed = function(knex, Promise) {
return Promise.all([
knex('users').del(),
knex('songs').del(),
]).then(() => {
return Promise.all([
knex('users').insert({username: 'u1', password: '1', access: 'regular'}),
knex('users').insert({username: 'u2', password: '1', access: 'regular'}),
knex('users').insert({username: 'a', password: '1', access: 'admin'})
])
}).then(() => {
return knex('playlists').del().then(() => {
return Promise.all([
knex('playlists').insert({playlist_name: 'p1', user_id: 2, create_date: '2016-03-07 10:00:00'})
.returning('playlist_id').then(function(ids) {
return Promise.all([
knex('songs').insert({song_name: 'Don\'t Blame Me', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
knex('songs').insert({song_name: 'Talking To The Moon', artist_name: 'Bruno Mars', album_name: 'It\'s Better If You Don\'t Understand', release_date: '2010-05-11', genre: 'Pop'}),
knex('songs').insert({song_name: 'Catch a Grenade (The Hooligans Remix)', artist_name: 'Bruno Mars', album_name: 'The Grenade Session', release_date: '2011-02-08', genre: 'Pop'}),
knex('songs').insert({song_name: 'Grenade', artist_name: 'Bruno Mars', album_name: 'The Grenade Session', release_date: '2011-02-08', genre: 'Pop'}),
knex('songs').insert({song_name: 'Grenade (Acoustic)', artist_name: 'Bruno Mars', album_name: 'The Grenade Session', release_date: '2011-02-08', genre: 'Pop'}),
knex('songs').insert({song_name: 'Grenade (Passion Pit Remix)', artist_name: 'Bruno Mars', album_name: 'The Grenade Session', release_date: '2011-02-08', genre: 'Pop'}),
])
}),
knex('playlists').insert({playlist_name: 'p2', user_id: 1, create_date: '2017-03-07 09:00:00'})
.returning('playlist_id').then(function(ids) {
return Promise.all([
knex('songs').insert({song_name: 'Uptown', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
knex('songs').insert({song_name: 'I\'m Goin\' In', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
knex('songs').insert({song_name: 'Calm', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
knex('songs').insert({song_name: 'Fear', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
knex('songs').insert({song_name: 'God\'s Plan', artist_name: 'Drake', album_name: 'Scary Hours', release_date: '2018-01-19', genre: 'Hip-hop'}),
knex('songs').insert({song_name: 'Diplomatic Immunity', artist_name: 'Drake', album_name: 'Scary Hours', release_date: '2018-01-19', genre: 'Hip-hop'}),
knex('songs').insert({song_name: 'Housetatlantavegas', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-09-15', genre: 'Hip-hop'}),
knex('songs').insert({song_name: 'Successful', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
knex('songs').insert({song_name: 'Best I Ever Had', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
])
}),
knex('playlists').insert({playlist_name: 'p3', user_id: 1, create_date: '2018-01-05 08:00:00'})
.returning('playlist_id').then(function(ids) {
return Promise.all([
knex('songs').insert({song_name: 'Look What You Made Me Do', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
knex('songs').insert({song_name: 'So it Goes...', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
knex('songs').insert({song_name: 'Gorgeous', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
knex('songs').insert({song_name: 'Getaway Car', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
knex('songs').insert({song_name: 'King of My Heart', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
knex('songs').insert({song_name: 'Dancing With Our Hands Tied', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
knex('songs').insert({song_name: 'Delicate', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
knex('songs').insert({song_name: '...Ready for it?', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
])
})
])
});
}).then(() => {
return knex('playlists_songs').del().then(() => {
return Promise.all([
knex('playlists_songs').insert({playlist_id: 1, song_id:1}),
knex('playlists_songs').insert({playlist_id: 1, song_id:2}),
knex('playlists_songs').insert({playlist_id: 1, song_id:3}),
knex('playlists_songs').insert({playlist_id: 1, song_id:4}),
knex('playlists_songs').insert({playlist_id: 1, song_id:5}),
knex('playlists_songs').insert({playlist_id: 1, song_id:6}),
knex('playlists_songs').insert({playlist_id: 1, song_id:7}),
knex('playlists_songs').insert({playlist_id: 1, song_id:8}),
knex('playlists_songs').insert({playlist_id: 1, song_id:9}),
knex('playlists_songs').insert({playlist_id: 1, song_id:10}),
knex('playlists_songs').insert({playlist_id: 2, song_id:11}),
knex('playlists_songs').insert({playlist_id: 2, song_id:12}),
knex('playlists_songs').insert({playlist_id: 2, song_id:13}),
knex('playlists_songs').insert({playlist_id: 2, song_id:14}),
knex('playlists_songs').insert({playlist_id: 2, song_id:15}),
knex('playlists_songs').insert({playlist_id: 2, song_id:16}),
knex('playlists_songs').insert({playlist_id: 2, song_id:17}),
knex('playlists_songs').insert({playlist_id: 3, song_id:18}),
knex('playlists_songs').insert({playlist_id: 3, song_id:19}),
knex('playlists_songs').insert({playlist_id: 3, song_id:20}),
knex('playlists_songs').insert({playlist_id: 3, song_id:21}),
knex('playlists_songs').insert({playlist_id: 3, song_id:22}),
knex('playlists_songs').insert({playlist_id: 3, song_id:23}),
])
});
})
};
运行knex seed:run时,应进入 p2 的歌曲最终会进入 p1 。
我该怎么做才能使种子有序运行?我尝试使用依次将播放列表链接在一起,但是仍然出现相同的问题(可能是链接不正确)。
所有建议将不胜感激。
谢谢!
答案 0 :(得分:1)
您应该使用bluebird
中的mapSeries来按顺序插入数据。
由于knex在后面使用了bluebird,因此您可以将其用作-
exports.seed = function (knex, Promise) {
// Deletes ALL existing entries
return knex('users').del()
.then(function () {
// Inserts seed entries
const users = [{ username: 'u1', password: '1', access: 'regular' }];
return Promise.mapSeries(users, (u) => {
return knex('users')
.insert(u);
})
});
};