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)
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.
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.
Should always be an int, no matter what else happens in the entity.
Actual Behavior
Given an entity definition that has
but also a one to one relationship where the JoinColumn references a column with a different type:
Now, when the
idis passed as a parameter in afindOnequery, it is passed as the type oflinkId. If as in this caseidis a number, butlinkIdin 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 areWhich 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
My Environment
Additional Context
No response
Relevant Database Driver(s)
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.