-
-
Notifications
You must be signed in to change notification settings - Fork 495
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
ValidationError when mapping double type data with query builder in v6 #5120
Comments
That's because doubles are mapped to string, not to number (at least I think so), your entity definition is not aligned with how it works and you get this as a result. If you want to map to number, provide a type that handles it, as the data from database for double/decimal/numeric/bigint will always be a string (and the ORM wont convert that unless you provide a type implementation that would do so). This worked the same in v5, such property was mapped to string there too, probably just the type in your entity def was ignored/overridden by the ORM automatically, now its respected (but wrong). We should probably handle this the same way as bigints now, allowing to map to number explicitly (and maybe even infer the type if available) edit: uh wait a sec, you say it works with |
Yep I tried to debug and I saw that it was mapped to string. I can understand the why for decimal/numeric/bigint, but for double I don't think we lose in precision ? (atleast in postgres that is what we are using). I thought it was a bug because like I said I don't have Now I'm curious how I need to handle my |
Hmm, I guess you are right, JS numbers are also double precision so it should be safe to map that. Ideally, this should also reflect the type you set in your entity definition, that way it will be non-breaking. Weird that the db drivers are returning strings here. Anyway, before this gets resolved, you should be able to do this to get the mapping to number consistently: import { Type } from './Type';
import type { Platform } from '../platforms';
import type { EntityProperty } from '../typings';
export class MyDoubleType extends Type<number, string> {
override getColumnType(prop: EntityProperty, platform: Platform) {
return platform.getDoubleDeclarationSQL();
}
override convertToJSValue(value: string): number {
return +value;
}
override compareAsType(): string {
return 'number';
}
} And use that: @Property({ type: MyDoubleType })
randomDouble!: number; |
Hey sry, Maybe it's a normal behavior, but just to let you know. It works well now with your fix. Thank you for that 👍 |
It's set by the metadata provider if available, the code you linked uses But I don't understand how is the Maybe retest that approach with 6.0.4 just for the sake of it, as I feel like both will work correctly now. |
Yep, with or without the customType solution, it works well with the |
Describe the bug
Hi,
There is a weird bug in v6 using query builder. I have
double
type data in my schema, and when using query builder, I got for example:ValidationError: Trying to set Object.randomDouble of type 'string' to '0.022' of type 'number'
What is weird is that it works fine using em
find
method, and in v5 I don't have this error.Reproduction
user.entity.ts
test.ts
logs
What driver are you using?
@mikro-orm/sqlite
MikroORM version
6.0.2
Node.js version
20
Operating system
mcr.microsoft.com/devcontainers/typescript-node:1-20-bullseye
Validations
The text was updated successfully, but these errors were encountered: