Skip to content
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

fix wrong code in polymorphic-associations.md #780

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions docs/associations/polymorphic-associations.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ abstract class AbstractComment<Attributes, CreationAttributes> extends Model<
> {
declare id: number;

@Attributes(DataTypes.STRING)
@Attribute(DataTypes.STRING)
@NotNull
declare content: string;

@Attributes(DataTypes.INTEGER)
@Attribute(DataTypes.INTEGER)
@NotNull
declare targetId: number;
}
Expand Down Expand Up @@ -72,17 +72,17 @@ This solution only requires a single table, to which we add multiple, mutually-e
class Comment extends Model<InferAttributes<Comment>, InferCreationAttributes<Comment>> {
declare id: number;

@Attributes(DataTypes.STRING)
@Attribute(DataTypes.STRING)
@NotNull
declare content: string;

@Attributes(DataTypes.INTEGER)
@Attribute(DataTypes.INTEGER)
declare articleId: number | null;

@BelongsTo(() => Article, 'articleId')
declare article?: Article;

@Attributes(DataTypes.INTEGER)
@Attribute(DataTypes.INTEGER)
declare videoId: number | null;

@BelongsTo(() => Video, 'videoId')
Expand Down Expand Up @@ -111,23 +111,23 @@ In this type of polymorphic association, we don't use foreign keys at all.
Instead, we use two columns: one to store the type of the associated model, and one to store the ID of the associated model.

As stated above, we must disable the foreign key constraints on the association, as the same column is referencing multiple tables.
This can be done by using the `constraints: false`.
This can be done by using the `foreignKeyConstraints: false`.

We then use [association scopes](./association-scopes.md) to filter which comments belong to which models.

```ts
class Comment extends Model<InferAttributes<Comment>, InferCreationAttributes<Comment>> {
declare id: number;

@Attributes(DataTypes.STRING)
@Attribute(DataTypes.STRING)
@NotNull
declare content: string;

@Attributes(DataTypes.STRING)
@Attribute(DataTypes.STRING)
@NotNull
declare targetModel: 'article' | 'video';

@Attributes(DataTypes.INTEGER)
@Attribute(DataTypes.INTEGER)
@NotNull
declare targetId: number;

Expand Down Expand Up @@ -156,7 +156,7 @@ class Video extends Model<InferAttributes<Video>, InferCreationAttributes<Video>
foreignKey: 'targetId',
// highlight-start
// Foreign Keys must be disabled.
constraints: false,
foreignKeyConstraints: false,
// This scope ensures that loading the "comments" association only loads comments that belong to videos.
scope: {
targetModel: 'video',
Expand All @@ -174,7 +174,7 @@ class Article extends Model<InferAttributes<Article>, InferCreationAttributes<Ar
as: 'articles',
},
foreignKey: 'targetId',
constraints: false,
foreignKeyConstraints: false,
scope: {
targetModel: 'article',
},
Expand Down
Loading