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

Replace findOne with findUnique #3697

Closed
matthewmueller opened this issue Sep 18, 2020 · 4 comments · Fixed by #4235
Closed

Replace findOne with findUnique #3697

matthewmueller opened this issue Sep 18, 2020 · 4 comments · Fixed by #4235
Assignees
Labels
kind/improvement An improvement to existing feature and code. tech/typescript Issue for tech TypeScript. topic: prisma-client
Milestone

Comments

@matthewmueller
Copy link
Contributor

matthewmueller commented Sep 18, 2020

Problem

Today findOne depends on the unique constraints of the database to guarantee we'll find 0 or 1 records. According to the documentation:

The findOne query lets you retrieve a single database record by ID or another unique attribute. You can use the select and include options to determine which properties should be included on the returned object.

prisma.users.findOne({
  where: {
    email: "alice@prisma.io"
  }
})

Many people have been confused by this. They expect findOne to be more loose. More like: "Find the first record that matches my conditions."

Suggested solution

We're adding a new function called findFirst to solve for "Find the first record that matches my conditions." Here's the issue: #3676.

We're also considering to eventually replace findOne with findUnique. We feel this is a better name for what this method does.

In the short-term, we'd add findUnique as an alias to findOne and add a deprecation warning to findOne. In a later release, we'd remove findOne.

Alternatives

Leave it as is.


🙏 We're very curious to hear your thoughts! Do you see this as a problem like we do?

@matthewmueller matthewmueller added topic: prisma-client kind/feature A request for a new feature. labels Sep 18, 2020
@matthewmueller matthewmueller changed the title Add findUnique as an alias to findOne and deprecate findOne. Replace findOne with findUnique Sep 18, 2020
@matthewmueller matthewmueller changed the title Replace findOne with findUnique Discussion: Replace findOne with findUnique Sep 18, 2020
@matthewmueller matthewmueller added kind/feedback Issue for gathering feedback. and removed kind/feature A request for a new feature. labels Sep 18, 2020
@matthewmueller matthewmueller changed the title Discussion: Replace findOne with findUnique Replace findOne with findUnique Sep 18, 2020
@beeman
Copy link

beeman commented Oct 15, 2020

Personally I have a preference for findOne over findUnique as it's sorter and I think it works as expected. I feel that TypeScript helps you realize it only works with unique fields.

@matthewmueller
Copy link
Contributor Author

matthewmueller commented Oct 23, 2020

Hey @beeman thanks for sharing your thoughts. I also really like short, concise names. Unfortunately, we've found that newcomers expect findOne to do what findFirst now does, so this adjustment could be a way to clear that confusion up.

@janpio janpio added tech/typescript Issue for tech TypeScript. team/product kind/improvement An improvement to existing feature and code. and removed kind/feedback Issue for gathering feedback. labels Oct 28, 2020
@janpio janpio added this to the Backlog 2.11.0 milestone Oct 28, 2020
williamluke4 added a commit that referenced this issue Nov 12, 2020
williamluke4 added a commit that referenced this issue Nov 12, 2020
williamluke4 added a commit that referenced this issue Nov 12, 2020
williamluke4 added a commit that referenced this issue Nov 12, 2020
williamluke4 added a commit that referenced this issue Nov 12, 2020
@rinogo
Copy link

rinogo commented Feb 8, 2022

So, with these changes in place, for queries for unique rows, what's the difference between findFirst() and findUnique()?

I ask because if the DB is already enforcing uniqueness, won't the results of findFirst() and findUnique() be identical?

There appears to be a difference in the signature for the calls - findUnique() offers fewer parameter options. From what I see, findFirst() does everything that findUnique() does and more.

Ultimately, what I'm trying to understand is:

When I'm working with a table that already has my desired unique constraints in place, should I be using findUnique() or findFirst() and why? Or, perhaps it doesn't matter?

@malbaugh
Copy link

I would also like some clarification on the purpose/ ideal use case of findUnique vs findFirst. I can't find any commentary on why these two methods exist as findFirst seems to do everything findUnique does. Are there performance differences? I haven't been able to find any difference in my personal test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/improvement An improvement to existing feature and code. tech/typescript Issue for tech TypeScript. topic: prisma-client
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants