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

Applying filter for type Array field #441

Closed
radsal opened this issue May 13, 2020 · 8 comments
Closed

Applying filter for type Array field #441

radsal opened this issue May 13, 2020 · 8 comments
Assignees
Labels

Comments

@radsal
Copy link

radsal commented May 13, 2020

Steps to reproduce

Create a model with property type array:
e.g:

"propertyKey": {
      "type": [
        "string"
      ]
    }

Insert some records.
Try to filter the record: {where: { propertyKey: "somevalue"}}

Current Behavior

It doesn't return any records for the filter.
However, using loopback-connector-mongodb@5.2.3 package returns the desired response.

Expected Behavior

It should return matching records.

Link to reproduction sandbox

https://github.com/radsal/loopback-sandbox

Additional information

darwin x64 10.19.0

loopback@3.27.0
loopback-boot@3.3.1
loopback-component-explorer@6.5.1
loopback-connector-mongodb@5.2.3
loopback-connector-postgresql@5.0.1

Related Issues

See Reporting Issues for more tips on writing good issues

strongloop/loopback#4272

@radsal
Copy link
Author

radsal commented May 13, 2020

Seems like the issue is that the column is created as type text:

CREATE TABLE "public"."teams" (
  "name" TEXT NOT NULL,
  "manager" TEXT NOT NULL,
  "players" TEXT,
  "id" SERIAL,
  PRIMARY KEY("id")

@dhmlau
Copy link
Member

dhmlau commented May 17, 2020

@agnes512, you were looking into filters recently. Could you please help? Thanks.

@agnes512 agnes512 transferred this issue from loopbackio/loopback-connector-postgresql May 19, 2020
@agnes512 agnes512 transferred this issue from loopbackio/loopback-connector-mongodb May 19, 2020
@agnes512
Copy link
Contributor

Hi, could you try to specify the dataType field of the property?

"propertyKey": {
      "type": [
        "string"
      ],
      "postgresql":{
            "dataType": "varchar[]",
       }
    }

After you do npm run migration, that column should have data_type ARRAY.

@radsal
Copy link
Author

radsal commented May 20, 2020

Hi, following your suggestion the column does have datatype character varying[] however the filter {where: { propertyKey: "somevalue"}} fails with parse error.
Is there an example for such filter?

@agnes512
Copy link
Contributor

@radsal I checked the package, and you are right, PostgreSQL connector doesn't support filtering on arrays, see #342. I thought the operator inq would work. But seems like it only works for non-array properties. And based on #428, I think array only works with CRUD operations.

I believe it's a valid use case, not sure how much effort it needs tho D:

For other filters, we currently added these pages under Querying data.

@agnes512
Copy link
Contributor

agnes512 commented May 21, 2020

For anyone would like to contribute: the where filter and also its inq operator don't handle the case for arrays, see code

For example, to return instances that its array contains any provided values in the filter, the connector currently uses IN operator in the SQL, e.g SELECT .. FROM "public"."customer" WHERE "description" IN (...) ORDER BY "id", where description has type array, which would cause Malformed Array Literal error.

For such cases for arrays, the SQL should be
SELECT * FROM "public"."customer" WHERE 'check'=ANY("description")
or
SELECT * FROM "public"."customer" WHERE "description" @> ARRAY['check']::varchar[]

@stale
Copy link

stale bot commented Jul 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 20, 2020
@stale
Copy link

stale bot commented Aug 8, 2020

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this as completed Aug 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants