Ich habe derzeit zwei Sammlungen, die Schools
und Contacts
sind. Die Schools
-Sammlung wurde bereits mit Tausenden von Daten gefüllt, während Contacts
eine neue Sammlung ist, die noch leer ist.Segel erstellen Daten in viele zu viele Beziehung
Jetzt gibt es eine Schemaänderung für beide. Hier ist das Schema Detail:
// ----------- 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
}
}
};
Und mein Code ist hier, wenn Sie einen neuen Kontakt erstellen:
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) });
});
Allerdings, wenn ich auf meine Datenbank überprüfen, contacts
wurde erstellt, aber ohne schools
Attribute. Und das verwandte schools
hat auch noch kein contacts
Attribut.
Ich habe Folgendes versucht;
newContact.schools.add([schoolId]);
// or --
newContact.schools.add(schoolObj);
// or --
newContact.schools.add([schoolObj]);
Ich habe versucht, das dominant
Attribut zwischen diesen beiden Sammlungen zu wechseln und wie die Ausführung umgekehrt Tun
school.contacts.add(newContact);
Ich habe auch diese Lösung und keine lucks versucht;
- 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)
Fehle ich etwas hier? Jeder Vorschlag wird geschätzt ..
ich so dumm bin .. Ich habe erkannt, dass die viele zu viele Beziehung in anderen Sammlungen. Danke für deine Antwort.. –