현재 Sequelize.js로 데이터를 시드하고 연결 ID에 하드 코딩 된 값을 사용하고 있습니다. 이것은 동적으로 올바르게 할 수 있어야하기 때문에 이상적이지 않습니다. 예를 들어, 사용자 및 프로필을 "has one"및 "belongs to"연결과 연관시킵니다. 반드시 하드 코딩 된 profileId
을 사용자에게 시드하고 싶지는 않습니다. 프로필을 만든 후에 프로필 씨드에서 오히려 그렇게하고 싶습니다. 프로필이 생성되면 동적으로 사용자에게 profileId
을 추가합니다. Sequelize.js로 작업 할 때 이것이 가능한가? 또는 Sequelize로 시드 할 때 하드 코드 연결 ID가 더 일반적입니까?동적 시딩을 속임수로 지정
아마도 나는 잘못 먹는 것에 대해 이야기 할 것입니까? Sequelize를 사용하여 마이그레이션 파일이있는 일대일 시드 파일을 갖춰야합니까? Rails에는 일반적으로 시드 파일이 1 개만 있습니다. 원하는 경우 여러 파일로 분할 할 수 있습니다.
일반적으로 여기서 안내 및 조언을 구합니다.
users.js
// User seeds
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkInsert('Person', [{
name: 'John Doe',
isBetaMember: false
}], {});
*/
var users = [];
for (let i = 0; i < 10; i++) {
users.push({
fname: "Foo",
lname: "Bar",
username: `foobar${i}`,
email: `foobar${i}@gmail.com`,
profileId: i + 1
});
}
return queryInterface.bulkInsert('Users', users);
},
down: function (queryInterface, Sequelize) {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkDelete('Person', null, {});
*/
return queryInterface.bulkDelete('Users', null, {});
}
};
profiles.js
// Profile seeds
'use strict';
var models = require('./../models');
var User = models.User;
var Profile = models.Profile;
module.exports = {
up: function (queryInterface, Sequelize) {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkInsert('Person', [{
name: 'John Doe',
isBetaMember: false
}], {});
*/
var profiles = [];
var genders = ['m', 'f'];
for (let i = 0; i < 10; i++) {
profiles.push({
birthday: new Date(),
gender: genders[Math.round(Math.random())],
occupation: 'Dev',
description: 'Cool yo',
userId: i + 1
});
}
return queryInterface.bulkInsert('Profiles', profiles);
},
down: function (queryInterface, Sequelize) {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkDelete('Person', null, {});
*/
return queryInterface.bulkDelete('Profiles', null, {});
}
};
당신은 그냥 모두 하드 코딩 for
루프 (좋지 않은)을 사용하고 볼 수 있듯이
감사합니다. @ simon.ro! 이것은 큰 도움과 내가 이걸로 어려움을 겪은 유일한 사람이 아니라는 것을 아는 것이 좋다. – bideowego