couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Cottlehuber <d...@muse.net.nz>
Subject Re: Duplication of unique keys
Date Tue, 21 Sep 2010 09:54:57 GMT
On 21 September 2010 18:40, BABALWA MAGALELA <208179585@cput.ac.za> wrote:
> You have two  Patient Docs, Patient1 and Patient2 which have embedded
> Indentifier Docs. Below are the Two Docs
>
> {
>  "_id": "1234",
>  "_rev": "123456",
>  "name": "Patient 1",
>  "gender: "MALE",
>  "indentifier": [
>   {"_id": "abc", "name": "nationalID"}  ]
> }
>
>
> {
>  "_id": "5678",
>  "_rev": "788748",
>  "name": "Patient 2",
>  "gender": "FEMALE",
>  "indentifier": [
>   {"_id": "abc", "name": "nationalID"}  ]
> }
>
>
> According to the CouchDB spec if the attribute is named  _id you can not
> have duplicates of that attribute  in the DB. But in this case you where
> able to save the indentifier value  of the both Patient 1 and Patient 2
> using the same _id value "abc". According to CouchDB this is supposed to
> generate a conflict of duplication because of the _id, but it is not
> happening. So what is going on here. You are basically try to make
> indentifier unique across all patient documents, since there is no way
> of making an attribute unique in CouchDB.
>
>
>>>> Dave Cottlehuber <dave@muse.net.nz> 09/17/10 11:23 AM >>>
> Hi Babawala
>
> If you are creating your own UUIDs or using the system generated one,
> doc.id should be enforced as unique. what exactly is being duplicated
> - got an example?
>
> A+
> Dave

Aaah OK. So these _id are in different scopes - they're not the same _id.
CouchDB is responsible to ensure that it's _id field is unique - the one in the
same "scope" as _rev.

Your job is to ensure whatever else in the doc in unique. this could
be done with
a validation function (which is run during save).

So it seems what you want to happen is this - you store the first
patient's details something like this:

{
 "_id": "abc",
 "_rev": "1-234fe3432343",
 "name": "Patient 1",
 "gender: "MALE",
 "national_id: "some_national_ID"
}

you can either use the autogenerated _id from CouchDB, or you can
assign your own directly.
the _rev is provided back to you when you PUT the doc into CouchDB.

Then later on somebody else tries to store this:

{
 "_id": "abc",
 "name": "Patient 2",
 "gender": "FEMALE",
 "national_id": "some_different_national_ID"}  ]
}

& you want CouchDB to reject the doc, as the _id is already assigned.

In your case, assuming national IDs are already assigned to everybody
(& one assumes they
are unique already) then this is an ideal candidate for using as your
primary _id -

just use the value of the person's National ID as the _id in each doc:

e.g.

{
"_id": "some_national_id",
"name": "patient 1",
"gender": "male",
## no need for another nested identifier field or _id
}

I hope that makes sense.

You can also try this out in futon - create a new document, into the
source view, save it, and add
additional fields one by one. You can add the first "name" field in,
but not the second one. You can add
the third line in as names.name doesn't clash -

{
   "_id": "140bb463c19ed9a020fc397b5c000c66",
   "name: "yo raccoon",
   "name: "yo fox",
   "names": ["name: "interpretative dance"]
}
Take a look at http://json.org/ and the gory details at
http://www.ietf.org/rfc/rfc4627.txt

"
2.2.  Objects

   An object structure is represented as a pair of curly brackets
   surrounding zero or more name/value pairs (or members).  A name is a
   string.  A single colon comes after each name, separating the name
   from the value.  A single comma separates a value from a following
   name.  The names within an object SHOULD be unique.
"

cheers
Dave

Mime
View raw message