node.js - find records in sequelize seeds -


i've been trying write seeds project, i've ran bit of snag.

i've got many-to-many relation users , roles table. so, when i'm seeding database need add record correct ids join table. in order need find user email , role name , ids , that's problem. can't find documentation on sequelize site. i'm using sequelize-cli seeding , migrating things. parameter queryinterface, can't find example or mention thing can do. simple examples got me through migrating (somehow) , able find on google.

i've resolved using "dirty trick" i'd say...

// user_seeds.js up: function (queryinterface, sequelize) {   return queryinterface.bulkinsert(table, [{     id: 1,     name: 'john doe',     email: 'john@doe.com',     created_at,     updated_at }], {});  // roles_seeds.js up: function (queryinterface, sequelize) {   return queryinterface.bulkinsert(table, [{     id: 1,     name: 'admin',     created_at,     updated_at   }, {     id: 2,     name: 'user',     created_at,     updated_at }]);  //user_roles_seeds.js up: function (queryinterface, sequelize) {     return queryinterface.bulkinsert(table, [{     employee_id: 1,     role_id: 1   }]); }, 

don't solution since may troublesome in future should i'd want run seeds again , forget how works. there should way me query database using queryinterface. wondering if 1 of had ran issue , solved it, if so, please share.

this might not best practice resolve issue, wasn't able find better , enough keep me satisfied.

basically did required actual model needed tables , use normal sequelize api find records.

so basically, punch

const { user } = require('./models');  up: function (queryinterface, sequelize) {   return user.findorcreate({     where: { email: 'john@doe.com' },     defaults: {       name: 'john doe'     }   }); 

and on , forth. hope helps someone, if has better way of doing this, please share.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -