Is there any way to apply unique constraint in couchdb?
Suppose I have a document which have some fields like email, emp_id, phone_number that needs to be unique throughout the document. I could not find any way. Anyone knows how to achieve this?
Any answer/suggestion would be appreciated.
after a lot of searching, I found that If you want to add a unique constraint to only one parameter you can set it as id. But If there are multiple parameter that needs to be unique, then this is not possible in couchdb
The only unique constraint in CouchDB is on the document ID. If you can put the unique components of your data in the document ID, then you have a uniqueness constraint.
Related
I'd like to add a constraint which enforces uniqueness on a column only in a portion of a table.
ALTER TABLE stop ADD CONSTRAINT myc UNIQUE (col_a) WHERE (col_b is null);
The WHERE part above is wishful thinking.
Any way of doing this? Or should I go back to the relational drawing board?
PostgreSQL doesn't define a partial (i.e. conditional) UNIQUE constraint - however, you can create a partial unique index.
PostgreSQL uses unique indexes to implement unique constraints, so the effect is the same, with an important caveat: you can't perform upserts (ON CONFLICT DO UPDATE) against a unique index like you would against a unique constraint.
Also, you won't see the constraint listed in information_schema.
CREATE UNIQUE INDEX stop_myc ON stop (col_a) WHERE (col_b is NOT null);
See partial indexes.
it has already been said that PG doesn't define a partial (ie conditional) UNIQUE constraint. Also documentation says that the preferred way to add a unique constraint to a table is ADD CONSTRAINT Unique Indexes
The preferred way to add a unique constraint to a table is ALTER TABLE ... ADD CONSTRAINT. The use of indexes to enforce unique constraints could be considered an implementation detail that should not be accessed directly. One should, however, be aware that there's no need to manually create indexes on unique columns; doing so would just duplicate the automatically-created index.
There is a way to implement it using Exclusion Constraints, (thank #dukelion for this solution)
In your case it will look like
ALTER TABLE stop ADD CONSTRAINT myc EXCLUDE (col_a WITH =) WHERE (col_b IS null);
I'm creating an Azure Search index and would like a field to be globally unique. Initially, I tried using the id field for this purpose, but it looks like certain characters are not allowed in the id field. Is it possible to define a field as globally unique at the time of index schema creation?
"key"- Unique identifier for documents within the index. Exactly one
field must be chosen as the key field and it must be of type
Edm.String.
https://learn.microsoft.com/en-us/azure/search/search-what-is-an-index#attributes
If you encounter characters not being supported in Azure Search, have a look at base64 encoding your field using field mappings.
I am learning how to use IBM Cognos and my first task is to create relationships between the tables I have uploaded into Cognos.
Basically, I am trying to tell Cognos to link the id column in the Person Table with the person_id and related_person_id columns in the Relationship Table, as shown here:
However, this does not seem possible since the Match Selected Columns button becomes disabled when I try to also link the related_person_id column.
The reason I need to do this is because person_id and related_person_id are foreign keys - they point to people in the Person Table and explain how they are related.
How can this be accomplished in Cognos?
Thank you.
You can have any number of matches. You need to match a single query item from each side for each match. IIRC, a query item can be used in multiple matches, although that would only be really helpful once relational operators are implemented.
It isn't clear if in your case you want to use person_id and related_person_id as a composite key or if you want a 1.n relationship between ID and person_id and some other relationship (n.1?) between ID and related person ID or if a 1.n relationship between ID and person_id would be sufficient to whatever you are trying to accomplish.
Editorial comment:
It would be really really nice if Cognos introduced relational operators Real Soon Now.
I'm looking to query my CouchDB in such a way that some of the fields in a document can be wildcards that match any key request.
Example:
function(doc) {
emit(doc.some_field, doc);
}
?key=100 would match both the document with some_field of 100 and of some_field value like *.
Is this possible? Is there a hack to do that?
As per the CouchDB documentation you can do:
?startkey="key"&endkey="key\ufff0"
to match key*.
From Couchdb wiki:
CouchDB actually stores the
[key,docid] pair as the key in the
btree. This means that:
you always know which document the key and value came from (it's exposed as the 'id' field in the view result)
view rows with equal keys sort by increasing docid.
So I don't think that wildcard fields used as a part of a key are possible because they are sorted. Suppose they are possible. Then if you try to query a key range from a view, rows with a wildcard will be returned with any key range. That means that they are everywhere. But that's impossible because they are sorted. That is a row with a wildcard is placed between a pair of other rows one of which has a greater key and the other a smaller one.
Disclaimer: I jumped to C# 2008 recently and SubSonic 3 (3.0.0.4) at the same time. I haven't used Linq for much of anything in the past.
Is there an easy way to use the foreign key display value for sorting, rather than the FK Id (which is numeric)?
I've added a new Find method in my ActiveRecord.tt to help with sorting based on a string field name but after doing some testing I realized that even though its working as it should be, I am not handling foreign key fields at all (they are just sorting by their value).
Even if I need to change how I am accessing the data it is early enough in the project to do that. Just looking for suggestions.
LINQ is your friend in this situation, you just need to join your two objects and then sort by the property from your foreign object:
var primaryObjectsSorted =
from primaryObjects in PrimaryObject.All()
join foreignObjects in ForeignObject.All()
on primaryObjects.ForeignId equals foreignObjects.Id
orderby foreignObjects.PropertyYouWantToSortOn
select primaryObjects;
So you have table A which has id of table B as a foreign key and you want to sort table A by the DisplayName column of table B rather than the id of table B?
The only way to achive this is by a join.
SELECT tableA.* FROM tableA INNLER JOIN tableB ORDER BY tableB.DisplayName
In SubSonic2 you can do that, and still be able to update your records if you use the DB.Select(...).ExecuteCollection() method.
I think this should be possible with subsonic3, too.
Howevery, if you don't use the foreign key and the display name is unique, you should just use this value as your foreign key.