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

Custom Types with SQL fragments ignored in populate with joined strategy #1594

Closed
haayman-imagem opened this issue Mar 24, 2021 · 5 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@haayman-imagem
Copy link

Describe the bug
I've defined a custom type but when using populate the 'convertToJSValueSQL' method isn't used to create the SQL statement

import { Geometry } from "geojson";
import { Type } from "@mikro-orm/core";
import { parse, stringify } from "wkt";

export class GeometryType extends Type<Geometry | undefined, string | undefined> {
  convertToDatabaseValue(value: Geometry | undefined): string | undefined {
    if (value === undefined || value === null) {
      return undefined;
    }

    return stringify(value);
  }

  convertToJSValue(value: string | undefined): Geometry | undefined {
    return value ? parse(value) : null;
  }

  convertToJSValueSQL(key: string) {
    return `ST_AsText(${key})`;
  }

  convertToDatabaseValueSQL(key: string) {
    return `ST_GeometryFromText(${key}, 4326)`;
  }

  getColumnType(): string {
    return "geometry";
  }
}

This should create SQL statements using 'st_AsText(geometry)'. However when using populate the SQL statement just says '"p1.geometry" as "p1__geometry"'

@haayman-imagem
Copy link
Author

never mind. I just found the convertCustomTypes option

@haayman-imagem
Copy link
Author

It turns out that the option convertCustomTypes doesn't work on findOne?

When I'm calling something like:

findOne(
  {code}, 
 {
    populate:['geometries'], 
    convertCustomTypes: true
  })

the resulting SQL statement doesn't contain 'ST_AsText(geometry)'

@B4nan
Copy link
Member

B4nan commented Mar 24, 2021

This is not how you report a bug. You need to provide actual reproduction, ideally as a test case, but one way or the other, it needs to be complete and verifiable.

Here are passing tests that do work with findOne:

https://github.com/mikro-orm/mikro-orm/blob/master/tests/features/custom-types.test.ts

However when using populate the SQL statement just says '"p1.geometry" as "p1__geometry"'

This sounds like you are using joined loading strategy? That is important thing to note, please provide something complete. Ideally try to extend the existing test to reproduce it.

@haayman-imagem
Copy link
Author

I'm sorry, I don't know how to set that up. But fortunately it was indeed fixed by setting the loading strategy to SELECT_IN

@B4nan
Copy link
Member

B4nan commented Mar 24, 2021

Let's keep this open, it sounds like a bug for sure.

@B4nan B4nan reopened this Mar 24, 2021
@B4nan B4nan added the bug Something isn't working label Mar 24, 2021
@B4nan B4nan changed the title Custom Types ignored in populate Custom Types with SQL fragments ignored in populate with joined strategy Mar 24, 2021
@B4nan B4nan closed this as completed in 527579d Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants