-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
findOrCreate does not eagerload associations on create #9042
Comments
Please provide me a SSCCE. It will help me understand your problem quickly. ( What is SSCCE ? You can find template for Sequelize here ) A few good example of SSCCE |
@sushantdhiman Can you just tell me what's missing? A similar issue was opened a long time ago #1711, and this comment said that it should be supported now, but it doesn't look like so. |
Just check given example and see what you are missing |
Conversation model import Sequelize from 'sequelize';
export default (sequelize, DataTypes) => {
let conversation = sequelize.define('Conversation', {
conversation_id: {
allowNull: false,
autoIncrement: false,
primaryKey: true,
type: Sequelize.BIGINT(11)
},
recipient_user_id: {
allowNull: false,
type: Sequelize.BIGINT(11)
},
sender_user_id: {
allowNull: false,
type: Sequelize.BIGINT(11)
},
created_at: {
allowNull: false,
type: Sequelize.BIGINT(11),
},
updated_at: {
allowNull: false,
type: Sequelize.BIGINT(11)
}
}, {
underscored: true,
timestamps: false,
tableName: 'conversation'
});
conversation.associate = models => {
conversation.hasMany(models.Chat, {
foreignKey: 'conversation_id',
targetKey: 'conversation_id'
});
conversation.belongsTo(models.User, {
as: 'recipient',
foreignKey: 'recipient_user_id',
targetKey: 'user_id'
});
conversation.belongsTo(models.User, {
as: 'sender',
foreignKey: 'sender_user_id',
targetKey: 'user_id'
});
};
return conversation;
}; user model import Sequelize from 'sequelize';
export default (sequelize, DataTypes) => {
let user = sequelize.define('User', {
user_id: {
allowNull: false,
autoIncrement: false,
primaryKey: true,
type: Sequelize.BIGINT(11)
},
first_name: {
type: Sequelize.STRING(70),
allowNull: false
},
middle_name: {
type: Sequelize.STRING(70),
allowNull: true
},
surname: {
type: Sequelize.STRING(70),
allowNull: false
},
email: {
type: Sequelize.STRING(255),
allowNull: false
},
password: {
type: Sequelize.STRING(255),
allowNull: false
},
sex: {
type: Sequelize.STRING('6'),
allowNull: false
},
profile_image: {
type: Sequelize.STRING(255),
allowNull: true
},
confirm_token: {
type: Sequelize.STRING(72),
allowNull: false
},
is_confirmed: {
type: Sequelize.BOOLEAN,
defaultValue: false,
allowNull: false
},
remember_token: {
type: Sequelize.STRING(50),
allowNull: true
},
created_at: {
allowNull: false,
type: Sequelize.BIGINT(11),
},
updated_at: {
allowNull: false,
type: Sequelize.BIGINT(11)
}
}, {
underscored: true,
timestamps: false,
tableName: 'user'
});
user.associate = models => {
user.hasMany(models.Conversation, {
as: 'received',
foreignKey: 'recipient_user_id',
targetKey: 'user_id'
});
user.hasMany(models.Conversation, {
as: 'sent',
foreignKey: 'sender_user_id',
targetKey: 'user_id'
});
user.hasMany(models.Contact, {
foreignKey: 'user_id',
targetKey: 'user_id'
});
user.hasMany(models.Service, {
foreignKey: 'user_id',
targetKey: 'user_id'
});
};
return user;
}; chat model import Sequelize from 'sequelize';
export default (sequelize, DataTypes) => {
let chat = sequelize.define('Chat', {
chat_id: {
allowNull: false,
autoIncrement: false,
primaryKey: true,
type: Sequelize.BIGINT(11)
},
conversation_id: {
allowNull: false,
type: Sequelize.BIGINT(11)
},
sender_user_id: {
allowNull: false,
type: Sequelize.BIGINT(11)
},
message: {
allowNull: false,
type: Sequelize.STRING(5000)
},
seen: {
allowNull: false,
type: Sequelize.BOOLEAN,
defaultValue: false
},
seen_at: {
allowNull: true,
type: Sequelize.DATE
},
created_at: {
allowNull: false,
type: Sequelize.BIGINT(11),
},
updated_at: {
allowNull: false,
type: Sequelize.BIGINT(11)
}
}, {
underscored: true,
timestamps: false,
tableName: 'chat'
});
chat.associate = models => {
chat.belongsTo(models.Conversation, {
foreignKey: 'conversation_id',
target_id: 'conversation_id'
});
chat.belongsTo(models.User, {
foreignKey: 'sender_user_id',
target_id: 'user_id'
});
};
return chat;
}; |
@sushantdhiman are those enough? |
@aprilmintacpineda Point is to produce shortest possible case so I can understand and help you, you are just pasting your project files and asking me is that enough :) Just open an editor and try to reproduce this issue as SSCCE, away from your project |
@sushantdhiman If I have that much time I would have investigated this issue and probably corrected it myself. I can make a workaround to make it work for my project, but of course, that's not good (from my POV). @mickhansen I think you can help out with. |
@aprilmintacpineda So you think we got spare time to solve "your issues" when you don't have enough time to properly present a test case :) |
@aprilmintacpineda yeah, better silently closing this |
It seems that you have had spare time to respond in the only wrong and selfish way, the opposite of trying to help. |
What are you doing?
I want
findOrCreate
to eagerload the associations. Eagerloading associations only works when the record exists, but when it does not exists, it creates the record but it does not eagerload the associations specified.What do you expect to happen?
It should eagerload the associations even when the record was newly created.
What is actually happening?
It does not eagerload the associations when the record was newly created.
Output
I would get an error saying
Unhandled rejection TypeError: Cannot read property 'map' of undefined
this is related tochats
property, it is undefined when the record was newly created byfindOrCreate
.here is an example output when the record was newly created.
As you can see,
Conversation.sender
,Conversation.recipient
, andConversation.Chats
are allundefined
. Here is an example output when the record exists, therefore, was not newly created.Dialect: mysql
Dialect version: I don't know what this is asking for..
Database version: mysql Ver 14.14 Distrib 5.7.20, for macos10.12 (x86_64) using EditLine wrapper
Sequelize version: ^4.31.2
Tested with latest release: No (If yes, specify that version)
The text was updated successfully, but these errors were encountered: