RE: Unique fields

From: Gentry, Michael (michael_gentr..anniemae.com)
Date: Tue Oct 05 2004 - 10:22:35 EDT

  • Next message: Stefaan Huysentruyt: "Re: Unique fields"

    I'm still not sure I'd want Cayenne to do the unique constraint. I
    could see an option for putting it in the model for the purpose of
    schema creation, but if Cayenne has to do the unique constraints it will
    have to A) do an extra SELECT before every insert, which will slow
    things down quite a bit, or B) have a copy of the entire DB in memory so
    it can look it up, which will slow things down quite a bit. The
    database server is already quite optimized at A and B, so I'd leave the
    constraint checking there when possible. I suppose for A, Cayenne would
    only have to do that if you had specified additional constraints/etc for
    your entity. You'd also have to handle multiple unique attributes (not
    related) and compound unique attributes (related).

    My approach for the e-mail address would be to use validateForSave() to
    query the table (if the object hasn't been inserted yet -- look at
    PersistenceState.NEW) and handle it there. That way you don't have to
    sort out the exceptions and can give a nice error message to the user.
    Being able to write these custom methods is one of the great advantages
    of the two-class approach.

    I guess we'll see what Andrus thinks ... :-)

    /dev/mrg

    -----Original Message-----
    From: Steve Wells [mailto:stwell..wnmail.net]
    Sent: Monday, October 04, 2004 9:11 PM
    To: Gentry, Michael; cayenne-use..bjectstyle.org
    Subject: RE: Unique fields

    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>, cayenne-user@objectstyle.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. :-)

    /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 : Tue Oct 05 2004 - 10:22:42 EDT