SailsJS / Waterline - How do I use a 'string' index in models and associations?

爷,独闯天下 提交于 2019-12-13 04:24:19

问题


Newbie question here.

According to https://github.com/balderdashy/waterline#indexing you cannot use a 'string' datatype as an index in Waterline due to issues with case insensitivity:

There is currently an issue with adding indexes to string fields. Because Waterline performs its queries in a case insensitive manner, we are unable to use the index on a string attribute. There are some workarounds being discussed but nothing is implemented so far. This will be updated in the near future to fully support indexes on strings.

The problems I'm concerned with are: performance; associations using 'string' keys aren't possible.

My questions are:

  1. Does it in fact index on strings if told to do so? If so, then what is the index based on: a) the original string as is; b) lowercase of the string; c) UPPERCASE of the string?
  2. If it doesn't index on strings at all, as is implied if you can assume that 'cannot use' means 'does not create', then does that mean that Waterline / database backend does a non-indexed record-by-record retrieve and compare? Seems highly inefficient, if so.
  3. Are 'string' and 'text' datatypes both affected or can I use 'text', in which case, question #2 needs to answered for 'text' datatypes, too.
  4. If you really can't use 'string', what should I use instead for foreign keys? An integer ID and a join table to bind another table with the string values? (Seems wasteful but workable for something like a keywords list).
  5. If, and when, will this 'issue' be fixed?
  6. What and/or where are the 'workarounds being discussed'?
  7. Is there a better ORM I should look at or just use native SQL methods (FYI, I'm using PostgreSQL).

My hoped for answer here is: 'string' can be indexed but queries against 'string' datatypes are performed using lowercase (or uppercase, but let me know which). As such, I can have a 'string' key so long as the case is correct.

Also, I assume 'string' and 'text' are effectively synonymous in Waterline, at least outside of the database's native context.

Thanks in advance for any help here.


回答1:


TL;DR: don't worry about it.

Two points to make here:

  1. The note you're referring to is really meant as an explanation for why the unique property doesn't (currently) work properly for string attributes.

  2. It applies mostly to MongoDB.

When you create associations between two models in Sails, primary keys are always referenced; the current version of Waterline doesn't support specifying a foreign key field. When Sails creates a table in your Postgres database, it will automatically add a PRIMARY KEY index to the model's primary key (id by default, or whichever attribute you nominate with the primaryKey property if you set autoPk to false for the model). This happens for any column type. The issue you referenced refers to custom indexes on string types. So, everything should work as you expect using string columns as keys in Postgres, including associations.



来源:https://stackoverflow.com/questions/25870024/sailsjs-waterline-how-do-i-use-a-string-index-in-models-and-associations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!