Skip to content

Column type is overwritten when JoinColumn references a column with another type (mssql) #12440

@sastcsgh

Description

@sastcsgh

Issue description

The actual column type definition of an entity is ignored when it is also declared as a JoinColumn that references a column with a different type

Expected Behavior

Whatever a Column's defined type is, for reasons of predictability, in my opinion it should always be this type when passed as a parameter.

@PrimaryGeneratedColumn()
public id: number;

Should always be an int, no matter what else happens in the entity.

Actual Behavior

Given an entity definition that has

@PrimaryGeneratedColumn()
public id: number;

but also a one to one relationship where the JoinColumn references a column with a different type:

@OneToOne((type) => ProductItemSubscription)
@JoinColumn({name: "id", referencedColumnName: "linkId"})
public _subscription: ProductItemSubscription

Now, when the id is passed as a parameter in a findOne query, it is passed as the type of linkId. If as in this case id is a number, but linkId in ProductItemSubscription is a varchar, the query tries to pass the number as a varchar and fails with an error "Validation failed for parameter '0'. Invalid string.". The parameters it passes are

parameters: [
    MssqlParameter {
      value: 2212,
      type: 'varchar',
      '@instanceof': Symbol(MssqlParameter),
      params: []
    }
  ],

Which shows the issue exactly.

I'm not trying to argue that this is a good pattern to use, in fact I think it definitely smells. However, I do think it is a bug that TypeORM takes the type of the referenced column of another entity over the definition in the entity itself.

Steps to reproduce

@Entity()
export class Vehicle {
	@PrimaryGeneratedColumn()
	public id: number;
    @OneToOne((type) => ProductItemSubscription)
    @JoinColumn({name: "id", referencedColumnName: "linkId"})
    public _subscription: ProductItemSubscription
}

@Entity()
export class ProductItemSubscription {
    @PrimaryGeneratedColumn()
	public id: number;
    @Column("varchar", { name: "linkId" , length: 24})
	linkId: string;
}

My Environment

Dependency Version
Operating System macOS 26.4
Node.js version 24.14.1
Typescript version 6.0
TypeORM version 0.3.28

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • capacitor
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions