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 Partial/Filtered Unique Index for PostgreSQL and SQL Server #3076
Comments
Slight update: my example |
Can you share the SQL of the |
How we're using this (we're currently moving over from Python/SQLAlchemy to Prisma): CREATE TABLE pages (
id SERIAL PRIMARY KEY,
staticId character varying NOT NULL,
isLatest boolean NOT NULL,
previousVersionId integer REFERENCES workflows(id),
# ...content/title/etc fields
);
CREATE UNIQUE INDEX pages_pkey ON pages(id int4_ops);
CREATE UNIQUE INDEX "idx_pages_unique_staticId" ON pages(staticId text_ops) WHERE isLatest = true; ReadingThe frontend gets the page for display via SELECT * FROM pages WHERE staticId = ${staticId} AND isLatest = true; UpdatingGiven a newly created page with a single revision:
One would create a new version of page BEGIN;
UPDATE pages SET isLatest = false WHERE id = ${currentPageId};
INSERT INTO pages ("staticId", "isLatest", "previousVersionId") VALUES (${staticId}, true, ${currentPageId});
COMMIT; Resulting in:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Just saw the second part of the original issue @bkrausz: Yes please 👍 |
@albertoperdomo This should be revisited now, it could start by just removing NULL from the index. |
Any news? |
On SQL Server these are called "Filtered Indexes" (via): https://docs.microsoft.com/en-us/sql/relational-databases/indexes/create-filtered-indexes?view=sql-server-ver15 |
FWIW, it seems #7263 (comment) the |
Partial indexes are not part of that preview feature, so I can not really say if this feature request here will come "soon". |
Note from Tyler: This is needed as a performance optimization for large databases.
|
Note: In a related issue, an alternative feature request was mentioned (of course full support for these indexes would be better) |
Any news? |
Yes, I posted a comment in a duplicate/related issue asking for SQL examples so we can design this feature better: #6974 (comment) Everyone who commented here or is subscribed to this issue, please go over there and comment. Thanks! And because these issues are effectively about the same feature, I am closing this one here now and ask you please to continue the discussion over at #6974. 🙏 |
Problem
tl;dr - Support for Postgres Partial Indexes and related functionality
Consider, for ex, a versioned CMS schema
New entries for the same page (i.e. identical
staticId
) are created by adding a new entry withisLatest=true
and settingisLatest=false
on the old one. We want to enforce that only one version is ever latest, so we have a partial index onstaticId
whereisLatest=true
.It looks like Prisma is mostly peaceful with this except for a few things:
@@unique
), which is incorrectpost.findOne
won't allow{where: {staticId: '...', isLatest: true}}
because there's no way to tell it that this guarantees uniqueness.Suggested solution
Partial index support ala
@@unique([staticId], where={isLatest: true})
would be great.Alternatives
I can probably work around this with
findMany(...)[0]
, but flagging as a feature request.Additional context
There's another challenge in supporting this data model: AFAICT there is no way to update an object at the same time as connecting it. It seems like right now I can run:
Whereas I'd ideally like to run:
Happy to file as a separate feature request as it seems mostly unrelated to partial indexes.
The text was updated successfully, but these errors were encountered: