sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

Naming collision between model attributes

Open mahmoudalsof opened this issue 3 years ago • 0 comments

Issue

Versions

  • sequelize: 6.6.5
  • sequelize-typescript: 2.1.3
  • typescript: 4.8.2

Issue type

  • [ ] bug report
  • [ ] feature request

Actual behavior

I have two models, which have an association between them. it seems the column name are clashing with eachother. I am converting project from js to ts and using sequelize-typescript I have checked the previous issues and tried looking all over the internet but I found a couple of unanswered SO questions.

Simplified version of my two models

user.model.ts

@Table({
    paranoid: true,
    underscored: true,
    modelName: "User",
    tableName: "users",
})

export class User extends Model {

    @HasMany(() => Address, "userId")
    addresses: Address[]

    @Column({
        type: DataType.STRING,
        allowNull: false,
    })
    uid: string

    @CreatedAt
    createdAt: Date

    @UpdatedAt
    updatedAt?: Date

    @DeletedAt
    deletedAt?: Date
}

address.model.ts

@Table({
    paranoid: true,
    underscored: true,
    modelName: "Address",
    tableName: "addresses"
})

export class Address extends Model {

    @BelongsTo(() => User, "userId")

    @Column({
        type: DataType.STRING,
        allowNull: false
    })
    uid: string

    @CreatedAt
    createdAt: Date

    @UpdatedAt
    updatedAt?: Date

    @DeletedAt
    deletedAt?: Date
}

This setup causes a clash between the two uid attributes:

throw new Error(`Naming collision between attribute '${association.as}' and association '${association.as}' on model ${association.source.name}. To remedy this, change either foreignKey or as in your association definition`);
[start:dev]     ^
[start:dev] Error: Naming collision between attribute 'uid' and association 'uid' on model Address. To remedy this, change either foreignKey or as in your association definition

I am not sure what I'm doing wrong. In the full code, if I comment out uid attributes in both models, the next attribute which exist in both models throws an error.

Expected behavior

Steps to reproduce

Related code

insert short code snippets here

mahmoudalsof avatar Sep 05 '22 16:09 mahmoudalsof