Re: Violation of PRIMARY KEY constraint

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

  • Next message: Andrus Adamchik: "Re: Violation of PRIMARY KEY constraint"

    I am still not convinced that this is something Cayenne should care
    about. See below.

    On Nov 22, 2003, at 5:22 PM, Leonardo R. Nunes wrote:
    > Humm ... so ... I'll need to create a cache for the new objects then.
    > I'll need do that because if I create one DataObject1 and before commit
    > this DataObject1 create a DataObject2 with the same PK (Example: a
    > driver's license number) I'll get a SQLException.

    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

    Taking a random number and then checking the database and memory to see
    if it is not used doesn't seem like a good solution to this problem. Or
    you can turn this problem around if using the id in a read-only fashion
    is acceptable (as it should be in most of PK cases), and use Cayenne to
    generate sequential ids for you. See item (1) in this message for
    suggestions on how to implement a "getID()" method:

    http://objectstyle.org/cayenne/lists/cayenne-user/2003/11/0068.html

    > I think that one kind of automatic PK checking would be a nice feature
    > in Caynenne.

    Again, I am not convinced that we need it. Consider what would such
    check do in case it finds a duplicate. Throw an exception? Well, this
    is what will happen anyway if you try to commit a duplicate key. So I
    don't see value in this feature.

    Andrus



    This archive was generated by hypermail 2.0.0 : Sat Nov 22 2003 - 16:38:46 EST