Relations and bigints
See original GitHub issueSo 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:
- Created 8 years ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.I had another look into this issue and managed to fix it.
Changes I made: Models:
Methods:
Response:
My guess: As the relation is done using bigints (both
entity_id
andsteam_id
are bigints) and the returnedentity_id
fromstats
loses precision when being parsed to an int, it can no longer be matched to thesteam_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