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
Custom Type with rawDefault
serializes wrong value (from returning clause / postgresql) .
#725
Comments
It's not about MikroORM, this is done by the https://github.com/brianc/node-pg-types#use
Uh, why? Result of returning clause is a value from DB and it needs to be converted to JS value. And that is what
Again, I don't understand why you think so, results of select clause are ran thru https://github.com/mikro-orm/mikro-orm/blob/master/packages/core/src/hydration/ObjectHydrator.ts#L28 Looking at the code, the returning clause is just reassigned to the entity directly, without using hydrator, so that is the problem here (it basically ignores the custom type definition). |
Fix will be part of |
I got it backwards in those words, but correct in the code, specifically: public convertToDatabaseValue(
value: unknown,
_platform: Platform,
): Maybe<string> {
if (value === undefined || value === null) {
return value;
}
// TODO: we shouldn't need this, as we're not serializing `Date`s to strings.
// but, it's necessary for the returning clause...
// also, despite returning this as a string, MikroORM loads it as a Date. What?
if (value instanceof Date) {
return DateTime.fromJSDate(value).toISO();
}
if (value instanceof DateTime) {
return value.toUTC().toISO();
}
throw ValidationError.invalidType(TimestampTZType, value, "JS");
} |
@B4nan also, looking at the code, I see the resolution. You weren't using the hydrator before. c5384b4#diff-3fb8c8b8cea3f5b7b71c8ba25585b38eR122 Thanks for the fix. |
Describe the bug
I have a custom type (where I use Luxon for dates). I also have a
createdAt
column with arawDefault: "now()"
.And, here's my model:
When creating an instance, I get this:
As you can see, a value for
createdAt
is not supplied.However, as we can see in my test code, the result of that
returning
clause is a JS Date(?!) and re-selecting the item returns an expected Luxon DateTime:Expected behavior
The result of a
returning
clause should not be run throughconvertToJSValue
(which is happening), but instead throughconvertToDatabaseValue
(like any otherselect
call... which isn't happening, as theDate
isn't coerced to aDateTime
.Additional context
Add any other context about the problem here.
Versions
The text was updated successfully, but these errors were encountered: