Skip to content

@JoinTable does not respect inverseJoinColumns referenced column width #6442

@michael-golden

Description

@michael-golden

Issue type:

[ ] question
[x ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[x ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[ x] 0.2.25

Steps to reproduce or a small repository showing the problem:

Specifying joinColumn in the @JoinTable() decorator does not pass the inverse join column referenced column width.

@Entity()
export class FooEntity {
    @PrimaryColumn({ type: "int", width: 10, unsigned: true, nullable: false })
    @Generated()
    public id: number;

    @ManyToMany(() => BarEntity)
    @JoinTable({
        name: "foo_bars",
        joinColumns: [
            {
                name: "foo_id",
            }
        ],
        inverseJoinColumns: [
            {
                name: "bar_id",
            }
        ]
    })
    public fooBars: BarEntity[];
}

@Entity()
export class BarEntity {
    @PrimaryColumn({ type: "int", width: 10, unsigned: true })
    @Generated()
    public id: number;

    @Column("varchar", { nullable: false, length: 50 })
    public code: string;
}

The JunctionEntityMetadataBuilder build() implementation does not pass the 'width' as an arg.

See: https://github.com/typeorm/typeorm/blob/master/src/metadata-builder/JunctionEntityMetadataBuilder.ts#L110-L125

suggested change:

options: {
    length: !inverseReferencedColumn.length
                        && (this.connection.driver instanceof MysqlDriver || this.connection.driver instanceof AuroraDataApiDriver)
                        && (inverseReferencedColumn.generationStrategy === "uuid" || inverseReferencedColumn.type === "uuid")
                            ? "36"
                            : inverseReferencedColumn.length, // fix https://github.com/typeorm/typeorm/issues/3604

    width: inverseReferencedColumn.width,

    type: inverseReferencedColumn.type,
    precision: inverseReferencedColumn.precision,
    scale: inverseReferencedColumn.scale,
    charset: inverseReferencedColumn.charset,
    collation: inverseReferencedColumn.collation,
    zerofill: inverseReferencedColumn.zerofill,
    unsigned: inverseReferencedColumn.zerofill ? true : inverseReferencedColumn.unsigned,
    name: columnName,
    nullable: false,
    primary: true,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions