fortunejs / fortune-postgres

Postgres adapter for Fortune.
MIT License
23 stars 21 forks source link

Cast integers when searching arrays #20

Closed itsravenous closed 7 years ago

itsravenous commented 8 years ago

I was trying to search via an array relationship, e.g:

// Training plan definition
module.exports = {
  title: { type: String },
  description: { type: String },
  image: { type: String },
  // Relationships
  users: { link: 'users', isArray: true, inverse: 'trainingPlans' }
};
store.find('training_plans', null, {
  match: {
    users: [1, 2]
  }
})

but I kept getting the following pg error:

error: operator does not exist: integer[] @> text[]

with the hint:

No operator matches the given name and argument type(s). You might need to add explicit type casts

This really doesn't feel like the right or complete solution - I'd rather check the type of the field than the value, and I feel like this might be an issue for other types too, but this is my workaround for the moment.

Using Postgres 9.5.

gr0uch commented 8 years ago

Looks alright to me. You probably don't need to qualify value % 1 === 0 since it would get cast to an integer anyways.

Since primary keys can only be strings or numbers in Fortune.js anyways, this should be fine.

itsravenous commented 8 years ago

Aah, I guess I was thinking there could be other, non-relationship use cases (maybe you want to search an decimal[] column). But if you're happy for this to just fix the common use case of filtering on an array relationship then great :)

I'll clean up the linting stuff anyway.

gr0uch commented 8 years ago

Actually that is a good point and I hadn't thought of that. The % 1 check makes sense in that case, as long as one can use an int to search in a decimal column.

itsravenous commented 8 years ago

I was actually thinking I didn't want to squish decimals used to search a decimal[] into ints, but yeah, that too!

gr0uch commented 7 years ago

I'll just merge this with the lint problems, fix them, and release it.

itsravenous commented 7 years ago

Nice one!