question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Relations and bigints

See original GitHub issue

So I ran into a problem today and for the life of me, I can´t figure out what I´m doing wrong.

DB: MySQL Tables:

CREATE TABLE IF NOT EXISTS `players` (
  `steam_id` bigint(20) unsigned NOT NULL,
  `name` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `players`
 ADD PRIMARY KEY (`steam_id`);

CREATE TABLE IF NOT EXISTS `stats` (
  `stat_id` int(10) unsigned NOT NULL,
  `entity_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `entity_type` enum('Player','PlayerInTeam','Team') DEFAULT NULL,
  `games_played` int(10) unsigned NOT NULL,
  `games_won` int(10) unsigned NOT NULL,
  `games_lost` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='contains stats for players and teams';

ALTER TABLE `stats`
 ADD PRIMARY KEY (`stat_id`)

The entity_id in stats is the steam_id from players

Models:

var Player = bookshelf.Model.extend({
  tableName: "players",
  idAttribute: "steam_id",
  stats: function () {
    return this.hasMany(PlayerStats, "entity_id");
}
});

var PlayerStats = bookshelf.Model.extend({
  tableName: "stats",
  idAttribute: "entity_id"
});

Methods/Functions:

player.findPlayerStatsById = function (id) {
  return new models.player({steam_id: id})
    .query(function (qb) {
      qb.select(knex.raw('CONVERT(steam_id, CHAR(25)) AS steam_id'));
    }).fetch({
      columns: ['stats_id'],
      withRelated: ['stats']
    });
};

Response:

{"steam_id":"76561198173039321","stats_id":43, "stats":[]}

Debug output:

{ method: 'select',
  options: {},
  bindings: [ '76561198173039321', 1 ],
  sql: 'select CONVERT(steam_id, CHAR(25)) AS steam_id, `stats_id` from `players` where `players`.`steam_id` = ? limit ?' },
{ method: 'select',
  options: {},
  bindings: [ '76561198173039321' ],
  sql: 'select `stats`.* from `stats` where `stats`.`entity_id` in (?)' }

SQL Log:

980 Query   select CONVERT(steam_id, CHAR(25)) AS steam_id, `stats_id` from `players` where `players`.`steam_id` = '76561198173039321' limit 1
979 Query   select `stats`.* from `stats` where `stats`.`entity_id` in ('76561198173039321')

The main problem is steam_id, as it´s a bigint and can´t be natively handled by JS.

NOTE: I can´t even get the exact steam_id without using select CONVERT(steam_id, CHAR(25)) AS steam_id, even though I read others

The sql query itself works fine and the correct id is being passed in, so I just don´t know what´s going wrong.

Addition

I tried it with 1 as the steam_id and it worked fine

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
dukeofsussexcommented, Mar 18, 2016

UPDATE

The solution to this issue was so obvious I have no idea how I could miss it: All that needed to be done was to set supportBigNumbers: true for the underlying MySQL NodeJS driver, which results in bigints being returned as strings. It wasn´t a Knex or Bookshelf issue after all. Hope this helps anybody who stumbles across this issue.

0reactions
dukeofsussexcommented, Jan 30, 2016

I had another look into this issue and managed to fix it.

Changes I made: Models:

var PlayerStats = bookshelf.Model.extend({
  tableName: "stats",
  idAttribute: "stat_id"
});

Methods:

player.findPlayerStatsById = function (id) {
  return new models.player({steam_id: id})
    .query(function (qb) {
      qb.select(knex.raw('CONVERT(steam_id, CHAR(25)) AS steam_id'));
    }).fetch({
      columns: ['stats_id'],
      withRelated: [{ stats: function(query) { 
        query.select(
          knex.raw('CONVERT(entity_id, CHAR(25)) AS entity_id'), 'entity_type', 'game_type', 'elo', 'games_played', 'games_won', 'games_lost'); 
       }}]
    });
  };

Response:

{
  "steam_id":"76561198173039321",
  "stats_id":43,
  "stats: [
    {
      "entity_id":"76561198173039321",
      "entity_type":"Player",
      "game_type":"5v5_ranked",
      "elo":1124,
      "games_played":223,
      "games_won":92,
      "games_lost":102
    },
    {
      ....
    }
  ]
}

My guess: As the relation is done using bigints (both entity_id and steam_id are bigints) and the returned entity_id from stats loses precision when being parsed to an int, it can no longer be matched to the steam_id and therefore the returned array is empty. But that´s only a guess, all I know is that the problem only occurs when using bigints.

SOLVED

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to use relations for `BigInt` ids · Issue #2231 - GitHub
I have the following models and tables: create table parent ( id big serial ); create table child ( id big serial, parent_id...
Read more >
'relation "bigint" does not exist' error using sqlc library with go
I'm using the sqlc library to generate go code from SQL queries. I have a query with a subquery like this: COALESCE(SUM(invoice_payments.total) ...
Read more >
BigInt - JavaScript - MDN Web Docs
BigInt values represent numeric values which are too large to be represented by the number primitive.
Read more >
BigInt - TC39
Let r be the BigInt defined by the mathematical relation r = n - ( d × q ) where q is a...
Read more >
Entity Relationship Diagram core - CADRE
paper_id : varchar rank : bigint doi: varchar doc_type: varchar paper_title: varchar original_title: varchar book_title varchar year varchar.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found