-
-
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
Hide junction table data from JSON #2541
Comments
passing { joinTableAttributes: [] } solved the issue. |
Where is this documented? |
@mlegenhausen it's likely not current, ideally i'd like to change this to |
That would be really nice :) |
Is this solutions already enabled? I can't figure out how to hide the IDs from junction table on the query options: {
attributes: [
[sequelize.fn('sum', 'minutes'), 'sum']
],
include: [
{
model: models.Project,
attributes: ['id', 'name']
},
{
model: models.EmployeeCalendar,
attributes: [],
where: {
date: {
$lte: filter.dateEnd,
$gte: filter.dateIni
}
},
through: {
attributes: []
},
include: [
{
model: models.Employee,
attributes: [],
where: {
email: jwtData.email
}
}
]
}
]
} The "through" is ignored. The result is: SELECT `TimeEntry`.*,
`EmployeeCalendars.EmployeeCalendarTimeEntry`.`idTimeEntry`,
`EmployeeCalendars.EmployeeCalendarTimeEntry`.`idEmployeeCalendar`
FROM (
SELECT `TimeEntry`.`id`,
sum('minutes') AS `sum`,
`Project`.`id`,
`Project`.`name`
...) AS 'TimeEntry'
... This is my junction table declaration: // EmployeeCalendar TimeEntry
var EmployeeCalendar_TimeEntry = sequelize
.define('EmployeeCalendarTimeEntry', {}, {
timestamps: false
});
Models.EmployeeCalendar.belongsToMany(Models.TimeEntry, {
through: EmployeeCalendar_TimeEntry,
foreignKey: 'idEmployeeCalendar'
});
Models.TimeEntry.belongsToMany(Models.EmployeeCalendar, {
through: EmployeeCalendar_TimeEntry,
foreignKey: 'idTimeEntry'
}); |
+1. However overriding toJson solved my issue. :) |
@mickhansen it would be great if we could specify this as a model-wide option, or even |
For anyone confused, WoLfulus's answer is correct, but specifically that option applies to the association getter. a.getBs({ joinTableAttributes: [] })... |
I can't get this to work, I have a many-to-many relationship, and I have some columns in my junction table like this:
and my query:
the result still contains the columns from the junction table (tenant_users) |
@escobar5 not sure where the problem is in your case, but the following code does what you need. I prefer using association aliases but it should work with model reference as well. // model definition
Server.belongsToMany(User, {
as: "administrators",
through: "server_administrator",
}); // query
Server.findAll({
include: [
{
association: "administrators",
through: {
attributes: [],
},
},
],
}); |
I would like to know if there is any way to configure this behaviour globally for whole Sequelize instance, or at least for any given association, so you don't have to specify it in every single query. When defining new models, you can use // we can do the following
const User = database.define(
"user",
{
id: {
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.TEXT,
},
},
{
defaultScope: {
attributes: {
exclude: ["createdAt", "updatedAt"],
},
},
},
); // but this is not available at the moment
Server.belongsToMany(User, {
through: "server_administrator",
defaultScope: {
attributes: {
exclude: ["createdAt", "updatedAt"],
},
},
}); Any ideas how to achieve this? |
@malyzeli What do you mean by "since apparently |
@papb I'm pointing out that the term In model definition context
class Project extends Model {}
Project.init({
// Attributes
}, {
defaultScope: {
where: {
active: true
}
},
scopes: {
activeUsers: {
include: [
{ model: User, where: { active: true }}
]
},
}
}); source: documentation on Scopes Setting This is something which is apparently not possible to preset on associations... In association context
Post.hasMany(Comment, {
foreignKey: 'commentable_id',
scope: {
commentable: 'post'
}
});
source: documentation on Scopes and Associations This is very different functionality than scopes in model definition! It feels like a naming collision and it might be quite confusing for new library users even though it is well described in the documentation. Apart from that I think this implementation of polymorphic associations is bad database design and shouldn't be used - well explained for example in this article. But that's just my opinion and fortunately that feature is optional, so you can still model your associations in a cleaner way... Anyway the main question still remains - is there a way to set something like |
You can use |
I have 2 tables that has a many-to-many association using a junction table. The junction table doesn't have any additional columns.
The setup is like this (the names are simplified just to illustrate:
I have an instance of a A model as "a" and from this instance I want to list all Bs associated to this instance. I do this with:
I was expecting something like
but it adds the junction table data to the result like this.
I couldn't find any way to hide this (mick suggested passing { attributes: [] } to the getBs() function but the result keeps the "a_b" with both a and b ids and the data that I want is gone.
Is this a bug? Is there any way to hide this without having to post-process the result?
This guy on stackoverflow has the same problem:
http://stackoverflow.com/questions/26362965/prevent-junction-table-data-from-being-added-to-json-with-sequelize
The text was updated successfully, but these errors were encountered: