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
Support sparse unique indexes #3419
Comments
I need more context indeed for this. I want to know whether this refers to multi column indexes or many uniques index on fields of the same model. |
My understanding: @sorenbs might be able to provide more context, he mentioned it. |
Is sparse unique indexes still on the agenda? I would really like to see that feature in Prisma. |
This is a very useful feature where a user can have either an email address or phone number as login and one of the 2 can be null. We still need a @unique index on these 2 fields. Currently, I add the sparse indexes manually, but it would be amazing if prisma supported them. |
I've also come up against this error. Sparse unique indexes in MongoDB enforce only non-null values to be unique which is a very handy feature. If i can help with this feature dev please let me know how Screen.Recording.2022-09-15.at.11.10.09.PM.movEdit: If I try and push the schema without the unique clause I get an error telling me one-to-one relations must use unique fields. So it's a bit of a catch 22 situation.
|
We will want to design this feature in a bigger context, and unfortuantely we are currently not set up great to accept contributions with our distributed codebase in Prisma Client and Prisma Engines. We are looking into adding some index options soon, so maybe this could be one of them. Can a field that has a sparse index also have other indexes (unique or normal)? What about this note from Mongo docs:
? We have a request for partial indexes already: #3076 |
Support for either would be great 😊
Implementing a clean method for defining a partial index in the prisma
schema would be harder than simply adding a flag to define an index as
@Spare (for partial it would also be needed to have a mechanism to specify
the filter, and be more complex in your db push/pull commands).
If I can help in any way please let me know.
…On Wed, 28 Sep 2022 at 22:58, Jan Piotrowski ***@***.***> wrote:
We will want to design this feature in a bigger context, and unfortuantely
we are currently not set up great to accept contributions with our
distributed codebase in Prisma Client and Prisma Engines.
We are looking into adding some index options soon, so maybe this could be
one of them.
Can a field that has a sparse index also have other indexes (unique or
normal)?
Seems yes since Mongo 5:
https://www.mongodb.com/docs/manual/core/index-sparse/#sparse-and-non-sparse-unique-indexes
What about this note from Mongo docs:
Changed in version 3.2: Starting in MongoDB 3.2, MongoDB provides the
option to create partial indexes
<https://www.mongodb.com/docs/manual/core/index-partial/#std-label-index-type-partial>.
Partial indexes offer a superset of the functionality of sparse indexes. If
you are using MongoDB 3.2 or later, partial indexes
<https://www.mongodb.com/docs/manual/core/index-partial/#std-label-index-type-partial>
should be preferred over sparse indexes.
? We have a request for partial indexes already: #3076
<#3076>
—
Reply to this email directly, view it on GitHub
<#3419 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIGDZ6EUEY6U27MO7UGVLLWASINBANCNFSM4QJ4ODHQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Right now the biggest help would be in understanding exactly how sparse works. From reading the docs you linked to it seems that it exists for both unique and non unique indexes, which points to an additional configuration Am I missing something else? |
This is where NoSQL and SQL differ slightly.
In SQL models a row must fill all values, any values that are 'empty' are
set to NULL. There is no concept of a missing/undefined value. There is either a value or its NULL.
By contrast in NOSQL a value can:
1) have a value
2) be null
3) not be provided / not exist (undefined)
A unique sparse index only enforces the index for the documents with the value.
Partial is similar but only enforces the uniqueness for the documents where the field value matches the filter.
Hope that helps?
https://www.mongodb.com/docs/manual/core/index-sparse/
I think support for sparse would be relatively straightforward in prisma,
simply parsing @Sparse in the schema for adapters that support it and
creating a key in the db with the sparse flag set. DB Pull would be
recognising a sparse key and writing it to schema.
What do you think @janpio ?
…On Thu, 29 Sep 2022 at 00:04, Jan Piotrowski ***@***.***> wrote:
Right now the biggest help would be in understanding exactly how sparse
works.
From reading the docs you linked to it seems that it exists for both
unique and non unique indexes, which points to an additional configuration
type for @unique, @@unique and @@Index. But at the same time you can have
both a sparse and a non sparse variant at the same time - which would clash
with how we currently define at least @unique. There your @Sparse
suggestions might be better - but of course that you would need to
differentiate between unique and index in there also seems wrong.
Am I missing something else?
—
Reply to this email directly, view it on GitHub
<#3419 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIGDZYPHC4UAMV7FICZLM3WASQDVANCNFSM4QJ4ODHQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
As sparse indexes can be either normal indexes or unique indexes, and both can exist on the same field, I do not think it is as simple. I am also not clear if this really justifies a full new attribute, instead of becoming a modifier - although it then also requires changing the rules for those so some use cases are possible. |
Interesting, how would a modifier work?
…On Thu, 29 Sept 2022 at 21:49, Jan Piotrowski ***@***.***> wrote:
As sparse indexes can be either normal indexes or unique indexes, and both
can exist on the same field, I do not think it is as simple. I am also not
clear if this really justifies a full new attribute, instead of becoming a
modifier - although it then also requires changing the rules for those so
some use cases are possible.
—
Reply to this email directly, view it on GitHub
<#3419 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIGDZ65VPFBAUQ5FVCGUJDWAXJCHANCNFSM4QJ4ODHQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
I meant something like |
That's a good proposal, seems a better way to go. 👌🏻
…On Thu, 29 Sep 2022 at 22:40, Jan Piotrowski ***@***.***> wrote:
I meant something like @unique(sparse: true) or similar. That would make
clearer how this is just a "variant" of a normal index/constraint.
—
Reply to this email directly, view it on GitHub
<#3419 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIGDZ3AJNCBC5BEO26GP4LWAXPC3ANCNFSM4QJ4ODHQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Yup, a paremeter seems better than another |
You can probably just create it manually on your collection and it should work. (Of course I have no idea what running What is a bit unknown and will need some research on our side, is how this influences non MongoDB databases. This issue is from a time when Prisma had no MongoDB support, and there was some idea that we would need to do something about it. (From the issue description it is clear that I was not the one knowing what they talk about here though.) |
Doing a quick test:
If a sparse index is set in mongodb (but not unique) `db push` removes the index.
If a unique sparse index is set in mongo AND defined as unique in the prisma schema then `db push` silently ignores
`Db pull` obviously doesn’t pick up sparsity
So in a nutshell, currently Prisma doesn’t support sparse indexes that are not unqiue but there is a workaround for unique sparse indexes in that if you set it manually using the `<Collection>_<field>_key` index naming convention it will preserve the index (since Prisma is currently blind to sparsity).
I have a question with the @unique(sparse: true) proposal, how would you ensure “non-unique sparse indexes” are preserved? Note that sparsity is separate from uniqueness.
|
|
Sounds good 👍🏻
…On Sat, 1 Oct 2022 at 02:18, Jan Piotrowski ***@***.***> wrote:
@@Index([...], sparse: true) would also need to exist I guess.
—
Reply to this email directly, view it on GitHub
<#3419 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIGDZ6UCNHFXTJ3HE2V4CLWA5RJRANCNFSM4QJ4ODHQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
I learned that sparse indexes are sometimes (when you request more documents than contained in the index) only used by queries with a |
@janpio I have real case from app. User can have property, eg.: telegram_id but it it optional. On the other hand I would to see error if second user will use the same telegram_id. Now I have two options: For me it would be enough if I would be able to describe this use case in schema and unblock: Even docs about recommended method of appending custom mongo indexes to prisma schema would be useful. Btw I know rust, typescript and prisma, so I can try to help with this issue if you will give me some introduction. |
Hey guys, is there any update on this? |
Also experiencing this issue, which is forcing us to create one-to-many relationships that realistically should be able to be one-to-one. something like |
I wanted to follow the instructions to create a optional 1:1 relation, but am on MongoDB. I'd say this just doesn't work on MongoDB due to the lack of the sparse index feature. Am I wrong? |
Another problem I face: declaring a unique index across optional fields makes them non-optional, for example in the where clause of an upsert, so I can't even work around this. |
Any update on this? I want to create a optional 1:1 relation using MongoDB and I can't do that at the moment |
I just started doing my first steps with Prisma and stuck with issue that is already 5 year here. Very sad |
It seems @janpio is going to finally resolve this. |
Do we have any updates on this? |
It literally took Mikro-ORM one line of code and one day from the issue to the PR to address this problem VS ... |
During the backend meeting it came up that we might want to test "sparse unique indexes". Backend team (probably @sorenbs) has more context.
The text was updated successfully, but these errors were encountered: