RE: Unique fields

From: Gentry, Michael (michael_gentr..anniemae.com)
Date: Mon Oct 04 2004 - 10:41:15 EDT

  • Next message: Mike Kienenberger: "Re: [ANN] Cayenne RC1 Release"

    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. :-)

    /dev/mrg

    PS. The validation mechanisms, while useful, aren't applicable here.

    -----Original Message-----
    From: Steve Wells [mailto:stwell..wnmail.net]
    Sent: Sunday, October 03, 2004 11:39 PM
    To: cayenne-use..bjectstyle.org
    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.

    Example:
    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
    constraint fails.

    Not sure if this is already possible using validation, couldn't see
    anything there.

    Thanks,

    Steve



    This archive was generated by hypermail 2.0.0 : Mon Oct 04 2004 - 10:41:22 EDT