Re: "Can't get primary key from temporary id" :(

From: Aristedes Maniatis (ar..sh.com.au)
Date: Tue Apr 07 2009 - 21:28:25 EDT

  • Next message: Weddle, Anthony: "General queries"

    On 08/04/2009, at 11:03 AM, Joseph Schmidt wrote:

    >> * write the record to database, fetch it back again and
    >> then you'll have the primary key
    > Than this is not the same transaction :(.

    Correct. This is a limitation of how databases work, not Cayenne. You
    can't have a primary key until you write the record, unless you do
    something tricky like using a nanosecond timestamp hashed with the MAC
    address of your machine and the number you first thought of.

    >> * create a relationship between A and B
    > In this particular case, there's no relationship because the
    > 'id' from entity A is written to a text field in entity B together
    > with other strings, e.g. "Entity A with #id added at #date..."
    > So it's impossible to map here a relationship :(.

    Therein lies the danger of exposing primary keys to the user. I'd
    recommend you keep keys for relating data and have another generated
    number for this sort of thing. Or else, map a relationship and have
    your text message:

    > Entity A added at #date...

    Then put the related id into another attribute.

    > In other usage scenarios I have, e.g. for entity C, there's no
    > Cayenne relationship because it would mean to connect it with every
    > other table.

    That might be a good case for using inheritance.

    >> If you find yourself using the primary keys directly very
    >> often, you are probably not using Cayenne in the manner it
    >> was intended. The idea is that it removes you from thinking
    >> about database-centric issues like primary keys and just
    >> think about objects and how they relate to each other.
    >> Mostly.
    > Well, I'm not sure about Cayenne, but the entire RESt architecture
    > is based around the idea of using 'id's to identify entities (but
    > not just RESt).
    > If Cayenne can't play simply with 'id's (and it's supposed to be
    > used without them) than I think it automatically excludes itself
    > from most web applications :(, since most of them rely on sending
    > the 'id's back and forth.

    Cayenne can quite happily play with ids if that is what you need. My
    point was that if you are accessing the ids directly and manually
    joining up records with them, then you may be missing some of the
    power of Cayenne. But without knowing the details, I couldn't say for
    sure.

    > Is there no other way to get those IDs?
    > How to do e.g. audit with Cayenne? Mostly the audit string or record
    > must be in the same transaction with with what they log.

    Look at the documentation for lifecycle events. That is probably the
    best solution for this.

    Ari Maniatis

    -------------------------->
    ish
    http://www.ish.com.au
    Level 1, 30 Wilson Street Newtown 2042 Australia
    phone +61 2 9550 5001 fax +61 2 9550 4001
    GPG fingerprint CBFB 84B4 738D 4E87 5E5C 5EFA EF6A 7D2E 3E49 102A



    This archive was generated by hypermail 2.0.0 : Tue Apr 07 2009 - 21:29:03 EDT