나는 현재 Schools
과 Contacts
의 두 가지 컬렉션을 가지고 있습니다. Schools
컬렉션에는 이미 수천 개의 데이터가 채워져 있으며 Contacts
은 아직 비어있는 새 컬렉션입니다.많은 관계로 데이터를 만드는 돛
이제는 두 가지 모두 스키마가 변경되었습니다. 다음은 스키마 세부의 : 새 연락처를 만들 때 여기
// ----------- Schools.js
module.exports = {
attributes: {
user: {
model: 'users'
// Của User đã tạo
},
essential: {
type: 'string'
// Lưu toàn bộ thông tin của essential
},
contacts: {
collection: 'contacts',
via: 'schools'
},
pipeline: {
type: 'string',
defaultTo: ""
},
status: {
type: 'integer',
defaultsTo: 1
// 0: un-active, 1: active
}
}
};
// ----------- Contacts.js
module.exports = {
attributes: {
title: {
type: 'string'
},
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
email: {
type: 'string'
},
position: {
type: 'string'
},
landlinePhone: {
type: 'string'
},
mobilePhone: {
type: 'string'
},
externalCompany: {
type: 'string'
},
schools : {
collection: 'schools',
via: 'contacts',
dominant: true
},
user: {
model: 'users'
},
activities: {
collection: 'activities',
via: 'contact'
},
status: {
type: 'integer',
defaultsTo: 1
// 0: remove, 1: active, 2: unactive
}
}
};
을 그리고 것은 내 코드입니다 : 그러나
Contacts.create(contactData).exec(function (e1, newContact) {
if (e1) {
console.log("e1 ::: ", e1);
};
// newContact is filled with contact new data..
console.log("Contact data has been created", newContact, schoolId);
// Adding schools to newContact (schoolId has a value here);
newContact.schools.add(schoolId);
// Save
newContact.save(function (err) { console.log('err', err) });
});
, 내 데이터베이스에 검사 할 때, contacts
은 만들었지 만 schools
속성없이되었습니다. 그리고 관련 schools
에는 여전히 contacts
속성이 없습니다.
나는 다음을 시도했다.
newContact.schools.add([schoolId]);
// or --
newContact.schools.add(schoolObj);
// or --
newContact.schools.add([schoolObj]);
나는 두 모음 사이의 dominant
속성을 전환하는 시도하고 실행 내가 이러한 솔루션없이 lucks을 시도했습니다
school.contacts.add(newContact);
, 같은 반전하고;
- http://sailsjs.com/documentation/concepts/models-and-orm/associations/many-to-many
- Sails.js How to insert data into a join table (Many to Many)
defaultTo
에 오타가 있습니다. 귀하의 답변 주셔서 감사합니다 .. –