Thanks for an excellent response Michael. I agree with what you are
saying, but what I was asking has a slight difference in that I dont
want to use natural keys, I do use and only use Cayenne managed keys (I
agree with the philosophy). But I do want to have the framework check
these "unique" fields before insert.
So in the example, I want a user to have an id as the managed/auto key
and emailAddress as a unique field. I guess there would also be cases
where I would want to mark multiple fields as unique, such as firstName,
lastName, phoneNumber (as a poor example), but dont want these as a
composite PK, I want to have another field, the Id, as the PK.
Hope this makes sense.
----- Original message -----
From: "Gentry, Michael" <michael_gentr..anniemae.com>
To: "Steve Wells" <stwell..wnmail.net>, firstname.lastname@example.org
Date: Mon, 4 Oct 2004 10:41:15 -0400
Subject: RE: Unique fields
Unless I don't understand what you are asking, I believe Cayenne already
completely supports this.
In the modeler:
* For your User DbEntity, mark emailAddress as the primary key
* For your User ObjEntity, sync with the DbEntity and add emailAddress
as an attribute
* Generate your classes
* In your database:
* Create a unique index on emailAddress
In your code (the registration page):
* Create and register a new User object with your DataContext
* Set their emailAddress/etc
* commitChanges() and be ready to catch and sort out the exception
Of course, I don't think natural keys should ever be used (I've ranted
on that a few times before), but that's a philosophical discussion and
not an answer to your question. :-)
PS. The validation mechanisms, while useful, aren't applicable here.
From: Steve Wells [mailto:stwell..wnmail.net]
Sent: Sunday, October 03, 2004 11:39 PM
Subject: Unique fields
I think it would be nice to be able to flag a set of columns that would
make each row unique (for insert), much like using natural keys. This
is a lazy way of saying, I dont want to have to check if a set of field
values exist in the table before I insert.
A user can register on a web site using their email address. Using
generated keys they can keep registering unless my code checks for the
existance of their email address before inserting.
I'd like to just mark the "email address" column as unique in the
modeller and have the DB layer throw an exception if the unique
Not sure if this is already possible using validation, couldn't see
This archive was generated by hypermail 2.0.0 : Mon Oct 04 2004 - 21:10:44 EDT