You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have three models in place Activity, Item, and Label. M:N relationship between Activity and Label M:N relationship between Item and Label
I also have an explicit junction model named LabelAssociations where I define all attributes. I explicitly defined this model to specify all of its foreign keys which in this case are labelId, activityId, and itemId.
(I need to mention that I have multiple successful M:N relationships that work properly. These relationships are usually set between just two models, not three as in the current issue).
Model definition
Activity model:
@Table({tableName: 'activities'})exportdefaultclassActivityextendsModel{
@PrimaryKey
@IsUUID(4)
@Default(DataType.UUIDV4)
@Column(DataType.UUID)declareid: string;
@AllowNull(false)
@Column(DataType.STRING)declarename: string;
@UpdatedAtdeclareupdatedAt: Date;
@CreatedAtdeclarecreatedAt: Date;
@BelongsToMany(()=>Label,{foreignKey: 'activityId',otherKey: 'labelId',through: 'labelAssociations',onDelete: 'CASCADE',onUpdate: 'CASCADE',})declarelabels: ReturnType<()=>Label[]>;// Declare 'super many-to-many' association to be able to query all associated subLabels
@HasMany(()=>LabelAssociations,{foreignKey: 'activityId',as: 'associatedSubLabels',onDelete: 'CASCADE',onUpdate: 'CASCADE',})declareassociatedSubLabels: ReturnType<()=>LabelAssociations[]>;
Item model:
@Table({tableName: 'items'})exportdefaultclassItemextendsModel{
@PrimaryKey
@IsUUID(4)
@Default(DataType.UUIDV4)
@Column(DataType.UUID)declareid: string;
@Column(DataType.STRING)declaretargetWord: string;
@Column(DataType.STRING)declarequestionPhrase: string;
@UpdatedAtdeclareupdatedAt: Date;
@CreatedAtdeclarecreatedAt: Date;// Associations
@BelongsToMany(()=>Label,{foreignKey: 'itemId',otherKey: 'labelId',through: 'labelAssociations',onDelete: 'CASCADE',onUpdate: 'CASCADE',})declarelabels: ReturnType<()=>Label[]>;// Declare 'super many-to-many' association to be able to query all associated subLabels
@HasMany(()=>LabelAssociations,{foreignKey: 'itemId',as: 'associatedSubLabels',onDelete: 'CASCADE',onUpdate: 'CASCADE',})declareassociatedSubLabels: ReturnType<()=>LabelAssociations[]>;
The .$add command on both instances won't ever find the activityId column in the LabelAssociations. It always just validates the itemId column.
** SQL error:**
STATEMENT: INSERT INTO"labelAssociations" ("createdAt","updatedAt","labelId","itemId") VALUES ('2024-03-26 09:33:54.651 +00:00','2024-03-26 09:33:54.651 +00:00','ffffffff-bbbb-bbbb-bbbb-aaaaaaaaaa21',NULL) RETURNING "createdAt","updatedAt","labelId","itemId"
ERROR: null value in column "id" of relation "labelAssociations" violates not-nullconstraint
I can see that the .$add command has one more parameter of type AssociationActionOptions. Is it possible to provide an alias there or even the foreignKey explicitly?
Any help would be appreciated. Thank you in advance.
The text was updated successfully, but these errors were encountered:
Issue
The
.$add
seems to be resulting in a faulty SQL query when there are multiple M:N relationships set on a junction table.The issue happens only inside of a Docker container,
localhost
works as expected.Versions
Dialect: postgres
Database version: 15
Sequelize version: 6.25.3
Sequelize-typescript version: 2.1.6
typescript: 5.2.2
Issue type
Context
I have three models in place
Activity
,Item
, andLabel
.M:N relationship between Activity and Label
M:N relationship between Item and Label
I also have an explicit junction model named
LabelAssociations
where I define all attributes. I explicitly defined this model to specify all of its foreign keys which in this case arelabelId
,activityId
, anditemId
.(I need to mention that I have multiple successful M:N relationships that work properly. These relationships are usually set between just two models, not three as in the current issue).
Model definition
Activity model:
Item model:
Label model:
LabelAssociations model:
Steps to reproduce
The
.$add
command on both instances won't ever find theactivityId
column in theLabelAssociations
. It always just validates theitemId
column.** SQL error:**
I can see that the
.$add
command has one more parameter of typeAssociationActionOptions
. Is it possible to provide an alias there or even theforeignKey
explicitly?Any help would be appreciated. Thank you in advance.
The text was updated successfully, but these errors were encountered: