Skip to content

Invalid type is passed for Generated Primary Column in case if CockroachDB #8532

@wodka

Description

@wodka

Issue Description

class A {
  @PrimaryGeneratedColumn() // adding { type: 'bigint' } fixes the problem
  @Generated('rowid')
  public id: string
}

class B {
  @OneToOne(() => A, { primary: true })
  @JoinColumn({ name: 'id' })
  public a: A

  @RelationId('a')
  public id
}

The default primary key for cockroach ist with int an 64 bit integer

Expected Behavior

loading now entity B with id 721339096980258817 works. I would now expect a.id to be "721339096980258817"

const a = repo.findOne(A, '721339096980258817')

console.log('a.id', a.id) // should output the string '721339096980258817'

Actual Behavior

loading now entity B with id 721339096980258817 works. but when I take a look at the value of id it will be converted to 721339096980259000 (cropped as a 32 bit integeR)

const a = repo.findOne(A, '721339096980258817')

console.log('a.id', a.id) // outputs the number '721339096980259000'

I've debugged it to know at least that the database layer returns the right string value, but in https://github.com/typeorm/typeorm/blob/master/src/driver/cockroachdb/CockroachDriver.ts#L384 the passed type is Number.

I could not figure out why this is set this way.

by passing the type to the generated column it will work correctly.

Steps to Reproduce

The issue seems to stem from the type that is inferred from the Referenced column in the OneToOne releationship.

My Environment

Dependency Version
Operating System
Node.js version 16.13.0
Typescript version 4.5.4
TypeORM version 0.2.40

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb yes
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres no
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

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

  • ✖️Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ 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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions