Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use relations for BigInt ids #2231

Open
bbatha opened this issue Feb 22, 2022 · 1 comment
Open

Unable to use relations for BigInt ids #2231

bbatha opened this issue Feb 22, 2022 · 1 comment
Labels

Comments

@bbatha
Copy link

bbatha commented Feb 22, 2022

I have the following models and tables:

create table parent ( id big serial );
create table child (
   id big serial,
   parent_id bigint not null references parent.id
);
export class Parent extends Model {
  readonly id!: bigint;
  // ...
  static relationMappings = {
    child: {
      relation: OwnedModel.HasManyRelation,
      modelClass: 'child/child',
      join: {
        from: 'parent.id',
        to: 'child.parentId',
      },
    },
  }
}

export class Child extends Model {
  readonly id!: bigint;
  // ...
  static relationMappings = {
    children: {
      relation: OwnedModel.BelongsToOneRelation,
      modelClass: 'parent/parent',
      join: {
        from: 'child.parentId',
        to: 'parent.id',
      },
    },
  }
}

When trying to do a related query:

  const latestChild = await parent
    .$relatedQuery<Child>('children')
    .orderBy('child.id', 'desc')
    .first();

Produces the incorrect sql, the number is incorrectly quoted in the in clause

select "child".* from "child" where "child"."parent_id" in ('155') order by "child"."id" desc

Changing the columns in the model to be number doesn't help either since the bigint id is converted to a string for returned model in the first query.

@lehni lehni added the bug label Apr 15, 2023
@lehni
Copy link
Collaborator

lehni commented Jun 30, 2023

If this is on PostgreSQL, then perhaps it's linked to this issue here:
knex/knex#387

Could you try to see if this suggestion helps?
knex/knex#387 (comment)

Or even better:
brianc/node-pg-types#78 (comment)

import { types } from 'pg'

// Use BigInt for 64-bit integers.
types.setTypeParser(20, BigInt)
// Use BigInt[] for 64-bit integer arrays.
const parseBigIntArray = types.getTypeParser(1016)
types.setTypeParser(1016, text =>
  parseBigIntArray(text).map(value =>
    value === null ? value : BigInt(value)
  )
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants