-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example for model relations #20
Comments
It seems I figured. |
This is more of a documentation issue but I've re-opened for visibility and created this issue to track actual progress: feathersjs-ecosystem/docs#51 |
I am running into a similar issue as @kulakowka - I keep getting a const tutorial = sequelize.import( path.join(__dirname, "../tutorial/tutorial-model") ); ... I'm not even calling |
I'm kinda lost here… Disclaimer: I'm a total noob when it comes to SQL and relational databases, I have some experience with Mongo and, like @kulakowka, I want to learn some Say I have a model for a artists and another for events. Events can have many artists associated to it… module.exports = function(sequelize) {
const event = sequelize.define('events', {
date: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
allowNull: false
},
title: {
type: Sequelize.STRING,
allowNull: false
}
}, {
freezeTableName: true
});
setImmediate(() => {
// This is probably wrong, but otherwise most models are undefined
// Also this, I believe, runs after event.sync() which might be a problem
event.hasMany(sequelize.models.artists, { as: 'artists' });
});
event.sync();
return event;
}; It's my understanding that Is there something I'm missing? I believe I would need to |
What's the correct way to do this? |
@mrpatiwi the problem is using the I opted to create a new file EDIT: Please note that my experience with both Feathers and Sequelize is extremely limited and what I did is, in no way, recommended nor endorsed by the Feathers team. Hell, I'm not even sure what I did is considered a best practice or anything like that, it just made sense to me. |
I'm working on an example this evening. |
My current approach: // src/services/organization/organization-model.js
const Sequelize = require('sequelize');
module.exports = function(sequelize) {
const organization = sequelize.define('organization', {
name: {
type: Sequelize.STRING,
allowNull: false,
},
}, {
classMethods: {
associate(models) {
organization.hasMany(models.user);
},
},
});
return organization;
}; // src/services/index.js
const Sequelize = require('sequelize');
const authentication = require('./authentication');
const user = require('./user');
const organization = require('./organization');
module.exports = function() {
const app = this;
const sequelize = new Sequelize(app.get('postgres'), {
dialect: 'postgres',
logging: false, // console.log,
});
app.set('sequelize', sequelize);
app.configure(authentication);
app.configure(user);
app.configure(organization);
// Setup relationships
const models = sequelize.models;
Object.values(models)
.filter(model => model.associate)
.forEach(model => model.associate(models));
sequelize.sync();
}; |
@mrpatiwi solid. Very similar to @DesignByOnyx's solution he put over here. |
I would think @mrpatiwi approach is in line with the squelize cli generator, right? |
Thanks @mrpatiwi. |
FYI - the new generators will build all of the association code for you. Among other things it puts models in their own folder (de-coupled from services) and will generate the code which calls
|
Yup @DesignByOnyx is totally right. I think we can actually close this now as that is the recommended way to set up model relations with Sequelize. |
Will the new generators introduce any issues with existing |
Would it be possible to have an example using the new generator of what goes in the associate function in the .model.js files? ptest1.model.js
ptest2.model.js:
|
ptest2.associate = function (models) {
this.belongsTo(models.ptest_1)
}; |
Adding the underline solved the problem!
…On Tue, Jun 4, 2019 at 10:17 AM David Luecke ***@***.***> wrote:
models will contain the models keyed by the string used in
sequelizeClient.define(name. In your case
ptest2.associate = function (models) {
this.belongsTo(models.ptest_1)
};
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#20?email_source=notifications&email_token=ACPEUFJVODVURLJ4IBPQ45DPY2PTJA5CNFSM4B4UAB62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW5IMWY#issuecomment-498763355>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACPEUFPUY3LXO7DXCNT5P7LPY2PTJANCNFSM4B4UAB6Q>
.
|
That underline solved the problem!
I would like it to be:
Does Sequelize or feathers do this through some kind of config setting, or should I do it by looping through the keys of each populated object, and creating the object myself?
can I populate all those ids? Then can I have a hook that populates any model from any Sequelise service? (BTW, I would really love if the generator made this include hook when you create a Sequelize model, then add it to any Sequelize service by default or through a prompt. I'm pretty sure getting associations is something just about everyone using Sequelize will do eventually. If they don't, then they could just not use that hook or delete it.) |
@frastlin Not sure if you figured either out yet. But for the first question, you need to provide the
|
That "nest" worked perfectly!
with:
For future reference, here is my file: include-hook.js (on find and get hooks):
|
OK, now I am having the same problem as @silvestreh and I am unable to add or get hasMany to add any ids to the hasMany list, and the hasMany list is not a list, it's a single object, similar to belongsTo. Are there any examples how to fix this? I followed all the syntax outlined in the manual but I still get "tests":{...null} Rather than creating a list of 2 entries, I get a repeated object with one entry in each object. This is not what I want at all. I also only seem to be able to add the parent id to the child, I can't add the child ID to the parent. I don't know if this is normal, but it makes associations a little funny. Here's my code:
|
One more question:
Can I have Sequelize handle this kind of relationship? When I try doing: ptest3.belongsTo(ptest3), it gives the error:
Here's my model file:
Edit: I figured out that if you do:
rather than
it will work. |
@silvestreh This is probably not the best way to do this, but to achieve the has many relationship in a list, I created an after hook that runs on get and find. The hook only runs if a before hook adds a "includes" attribute to context.
|
@frastlin Are you now more confident how the association/relation stuff works? I have serious trouble in understanding what I am doing wrong. I set up a test repo with my code https://github.com/Orbitkorbi/feathers-sequelize-test hoping you may have the one or other minute to have short look into it. Thanks |
Can somebody show an example of creating relations between the models?
I want to relate a model
Tutorial
andComment
. How can i do that?I make it so:
But sometimes, when i start my application i have error:
I used to work only with mongoDB. But I think I need to learn sequelize.
It would be great if we had more examples that show how to work with the database, how to set up the relationship between models and between services. I read the documentation, but it very much and I'm not like I can not configure it to work properly.
The text was updated successfully, but these errors were encountered: