Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 519879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:05:13+00:00 2026-05-13T08:05:13+00:00

Are there any techniques/proposals to enforce unique constraints? Yes, we can create key that’s

  • 0

Are there any techniques/proposals to enforce unique constraints? Yes, we can create key that’s unique, but we cannot change key and keys and also this approach is not suitable for complicated validation (separate unique login, separate unique email, etc…)

For example, an Account should have
unique login and email. Deriving a key
from this fields will result in
inconsistency:

key1: "Account-john@example.net-john", { email: "john@example.net", login: "john"}
key2: "Account-mary@example.net-mary", { email: "mary@example.net", login: "mary"}

Looking good, but:

key1: "Account-john@example.net-mary", { email: "john@example.net", login: "mary"}
key2: "Account-mary@example.net-mary", { email: "mary@example.net", login: "mary"}

Oops, now we have 2 accounts with login: “mary”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:05:13+00:00Added an answer on May 13, 2026 at 8:05 am

    This is one of the less fun bits of CouchDB. The best way I’ve found for handling unique fields that can change (like in your user example) is to create “pointer” documents with the unique values as a component of the key, and then use those to let you claim unique values. The important part of doing this is having a predictable key for the primary document, then saving the unique field claim documents prior to saving the primary (and letting key conflicts prevent the primary document from saving).

    Given a user with a unique username and unique email, your primary documents might look like this:

    user-1234: { username: "kurt", email: "kurt@localhost" }
    user-9876: { username: "petunia", email: "pie@crust.com" }
    

    The unique field pointers would look something like this:

    user-username-kurt: { primary_doc: "user-1234" }
    user-email-kurt@localhost: { primary_doc: "user-1234" }
    user-username-petunia: { primary_doc: "user-9876" }
    user-email-pie@crust.com: { primary_doc: "user-9876" }
    

    Creating or updating a user would take these steps:

    1. Prep your user document, generate a key for it if necessary
    2. Save a “pointer” document for each changed unique field
    3. If saving any of those fails, stop and fix errors
    4. Save primary user document

    Step 3 will take some thought. For instance, you won’t want to try claim unique values for fields that haven’t changed. You could, but then you’d have to put some additional logic in to handle a case where you’re claiming a value for a user that already owns that value.

    Step 3 would be a good place to let people take old claimed values as well. If one user has “released” the username kurt, for instance, I can just update that particular document to point to my new key after verifying that it’s no longer in use. The alternative is to clear out claimed unique values when they change. I’m not sure which would be less work, really. Leaving stale claimed values in makes the most sense to me.

    The interesting thing about this solution is that you don’t need to use those pointer documents for anything once they’re created. You can create views as normal on your user docs, and use them to query by email or username.

    This setup also allows you to do relationships without having to worry about cascading user keys. I don’t know about you, but my user documents are referenced by just about every other document in the system. Changing a user key would be a massive pain in the ass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any tools / XSLT style sheets around for transforming the XML execution
I'm aware that there probably isn't a perfect solution to my question (this sounds
I have a rails 3 application on localhost, but my host (dreamhost) uses 2.3.5.
I am writing some code that spawns quite a few threads (about 512 at
I'm wondering how people test artificial intelligence algorithms in an automated fashion. One example
For a Windows Phone 7 app, I have created an application bar which is
As the title says, I'm looking for the fastest possible way to write integer
A few clients have asked to make some static websites because their hosting doesn't
I don't run a mission critical web site so I'm not looking for an
I'm doing a lot of keyframe masking of vines/flowers for a project I'm working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.