Re: Violation of PRIMARY KEY constraint

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Sat Nov 22 2003 - 16:56:36 EST

  • Next message: Leonardo R. Nunes: "RE: Violation of PRIMARY KEY constraint"

    On Nov 22, 2003, at 4:38 PM, Andrus Adamchik wrote:

    > If you are assigning primary keys yourself in the application, you
    > better implement some algorithm that generates unique ids following
    > certain rules (this seems the most appropriate approach to me in case
    > of drivers licenses at least). E.g. something that this class does:
    >
    > http://objectstyle.org/cayenne/api/cayenne/org/objectstyle/cayenne/
    > util/IDUtil.html

    BTW, I just got an idea how to leverage Cayenne key auto-generation
    feature in implementing such algorithm. If we have the following
    starting points:

    * The requirement to generate a globally unique String or number for a
    given DB table, that is NOT SEQUENTIAL (and probably evenly
    distributedr).
    * Existing Cayenne feature that provides a guaranteed unique SEQUENTIAL
    number for the PK.

    Combining the two together we can create a PkGenerator implementation:

    http://objectstyle.org/cayenne/api/cayenne/org/objectstyle/cayenne/dba/
    PkGenerator.html

    That first fetches this number from the database as it normally does,
    but before returning it to the user, applies some algorithm appropriate
    for a given case, using fetched number as a "seed" of such algorithm
    that guarantees global uniqueness. Such custom PkGenerator can be done
    as a delegating wrapper on top of any existing PkGenerators that come
    with DbAdapter.

    If anyone is interested in an example of how to write and install such
    wrapper, I will try to write one and post it on this list.

    And hopefully in Cayenne 1.1 we may implement such functionality using
    simpler means of DataContextDelegate (that does not exist in 1.0.x)

    Andrus



    This archive was generated by hypermail 2.0.0 : Sat Nov 22 2003 - 16:56:40 EST