Knex种子:从承诺插入无序(Knex Seed: insert out of order from

2019-09-28 22:04发布

我想我的种子数据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种子:运行,歌曲,应该进入P2结束到P1。

我应该怎么做,使顺序运行的种子? 我试图链接播放列表一个又一个与当时的但同样的问题仍然存在(可能是我链接错误)。

所有的建议,将不胜感激。

谢谢!

Answer 1:

您应该使用从mapSeries bluebird其插入订单数据。 由于knex是使用蓝鸟的背后,你可以使用它原样

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


文章来源: Knex Seed: insert out of order from promise