2015-03-03 13 views
5

मैं नोड, एक्सप्रेस और अनुक्रमित के साथ एक साधारण डेटाबेस बना रहा हूं। मैंने अपने मॉडल बनाए हैं, और मेरे डेटाबेस में टेबल बनाए गए हैं।कई लोगों को सीक्वेलिज़ करें - नया रिकॉर्ड कैसे बनाएं और तालिका में शामिल हों अपडेट करें

मेरे पास कई सारे रिश्तों के साथ मॉडल उपयोगकर्ता और शहर हैं। Sequelize उपयोगकर्ता, शहरों और एक तालिका में शामिल होने वाले टेबल टेबल उपयोगकर्ता बनाया: UserId और CityId के साथ।

मेरा सवाल यह है कि जब मैं एक नया उपयोगकर्ता बनाता हूं तो मैं तालिका में शामिल होने के तरीके को कैसे अपडेट करूं? सिटीआईडी ​​संपत्ति को बनाने पर नजरअंदाज कर दिया जाता है।

//Models use 
    //City.hasMany(User); 
    //User.hasMany(City); 

    var user = User.build({ 
     first_name: 'John', 
     last_name: 'Doe', 
     CityId: 5 
    }); 

    user.save(); 
+0

ध्यान दें कि अनेक-से-अनेक संबंध के लिए, आप 'belongsToMany' संघों के लिए उपयोग करना चाहेंगे:' City.belongsToMany (उपयोगकर्ता, {के माध्यम से: UserCity}) ' –

उत्तर

4

प्रलेखन में आगे बढ़ने के बाद, मुझे विश्वास है कि मुझे जवाब मिल गया है।

कई रिश्ते अनुक्रमित करने के लिए कई बनाने के दौरान प्रत्येक मॉडल में विधियां सेट, सेट और जोड़ती हैं।

डॉक्स कई लोगों के लिए यह सोचते हैं मॉडल उपयोगकर्ता और परियोजना कई के साथ से: http://docs.sequelizejs.com/en/latest/docs/associations/#belongs-to-many-associations

यह उपयोगकर्ता के लिए परियोजना, और getProjects, setProjects और addProject तरीके getUsers, setUsers, addUsers जोड़ देगा।

तो मेरे मामले में मैंने किया था निम्नलिखित जहां "शहर" एक विशिष्ट शहर मॉडल City.find से लौटे ...

//user.setCities([city]); 

models.User.find({ where: {first_name: 'john'} }).on('success', function(user) { 
    models.City.find({where: {id: 10}}).on('success', function(city){ 
    user.setCities([city]); 
    });  
}); 
+0

अरे RickT आप कर सकते थे कृपया मेरे सवाल पर नज़र डालें? यह बहुत समान है और मुझे लगता है कि आप मदद करने में सक्षम हो सकते हैं। मैं वास्तव में इसकी सराहना करता हूं। http://stackoverflow.com/questions/29247408/updating-a-many-to-many-join-table-using-sequelize-for-nodejs – wusauarus

0

डॉक्स v3 से है:

// Either by adding a property with the name of the join table model to the object, before creating the association 
project.UserProjects = { 
    status: 'active' 
} 
u.addProject(project) 

// Or by providing a second argument when adding the association, containing the data that should go in the join table 
u.addProject(project, { status: 'active' }) 


// When associating multiple objects, you can combine the two options above. In this case the second argument 
// will be treated as a defaults object, that will be used if no data is provided 
project1.UserProjects = { 
    status: 'inactive' 
} 

u.setProjects([project1, project2], { status: 'active' }) 
// The code above will record inactive for project one, and active for project two in the join table 
0

शहर और उपयोगकर्ता मॉडल दोनों बनने के बाद आप शामिल तालिका के रूप में उपयोग किए गए मॉडल का एक नया उदाहरण बना सकते हैं।

const User = sequelize.define('user') 
const City = sequelize.define('city') 
const UserCity = sequelize.define('user_city') 

User.belongsToMany(City, { through: UserCity }) 
City.belongsToMany(User, { through: UserCity }) 

Promise.all([User.create(), City.create()]) 
    .then(([user, city]) => UserCity.create({userId: user.id, cityId: city.id})) 
संबंधित मुद्दे