-
Notifications
You must be signed in to change notification settings - Fork 281
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
Polymorphic Through #160
Comments
Update @HasMany({
() => User,
through: () => Subscription,
constraints: false,
scope: { targetType: "USER" },
}, "targetId")
usersSubscribedTo: IUser[]; or like this but not knowing how to scope it: @BelongsToMany(
() => User,
() => Subscription,
"userId",
"targetId",
)
usersSubscribedTo: IUser[]; |
So you want to implement a many-to-many relation, where the subscription table is the through table? |
Correct. So that ultimately I can query things like:
Currently I'm making this queries independently and explicitly in service functions, so it's really not a big issue, but I was just wondering if this type of query can be configured through decorators. |
Did you tried something like: @Table
export class User extends Model<User> {
@BelongsToMany(() => User, () => Subscription, 'subscriberId', 'userId')
usersSubscribedTo: User[];
}
@Table
export class Subscription extends Model<Subscription> {
@PrimaryKey
@ForeignKey(() => User)
@Column
subscriberId: number;
@PrimaryKey
@ForeignKey(() => User)
@Column
userId: number;
} |
If you remove the |
Ah, sry, my fault. This would be the proper way then: @Table
export class User extends Model<User> {
@Column
name: string;
@BelongsToMany(() => User, {
through: () => Subscription,
foreignKey: 'subscriberId',
otherKey: 'targetId',
constraints: false,
foreignKeyConstraint: true,
scope: {
targetType: 'user'
}
})
usersSubscribedTo: User[];
@BelongsToMany(() => Post, {
through: () => Subscription,
foreignKey: 'subscriberId',
otherKey: 'targetId',
constraints: false,
foreignKeyConstraint: true,
scope: {
targetType: 'post'
}
})
postsSubscribedTo: Post[];
}
@Table
export class Subscription extends Model<Subscription> {
@PrimaryKey
@ForeignKey(() => User)
@Column
subscriberId: number;
@PrimaryKey
@Column
targetId: number;
@Column
targetType: string;
} But when querying the data, the created sql string doesn't seem to work unfortunately. I will look into this soon. |
What I can already say is, that querying data with this model configuration doesn't work with pure sequelize, too. |
OK, it is possible. But therefor it is necessary to set through options to the belongsToMany options, wich is currently not possible with sequelize-typescript. See #122 |
I see. I appreciate you looking into this in such a short time @RobinBuschmann :) |
|
Using:
"sequelize-typescript": "0.4.0-beta"
I'm having some trouble figuring out how to do polymorphic model definitions so that the queries work as expected. I'm trying to do a Subscription table eg:
I'm focusing on getting USER to work first. I want to be able to get all the users a particular user has subscribed to. So I have something like this:
This looks kind of ok to me, but with this I will get:
The text was updated successfully, but these errors were encountered: