I was converting an older table definition from using an integer
primary key to a string (representation of UUID), and ran into a bit
of strange behavior, where my object instance's String primary key
receives an integer value (which appears to be the internal sqlite
rowid) after a flush.  From prior reading, I believed that I ought to
be able to use a non-integer primary key.

I was using 0.3.8 when I first run into this, but it appears to hold
true for the current SVN trunk as well.

It may just be with the sqlite layer (the DB I've been using and the
only one I have handy), or it may be an issue with handling default
values for primary key columns, I'm not sure.

For a short sample exhibiting the problem:

          - - - - - - - - - - - - - - - - - - - - - - - - -

import uuid
from sqlalchemy import *
from sqlalchemy.orm import *

engine = create_engine('sqlite:///')

meta = BoundMetaData(engine)

def default_uuid():
    return str(uuid.uuid4())

test_table = Table(
    'test', meta,
    Column('uuid', String, default=default_uuid, primary_key=True),
    )

class Test(object):
    pass

test_mapper = mapper(Test, test_table)


if __name__ == "__main__":

    meta.create_all()
    s = create_session(engine)

    test = Test()

    # Works if the line below is uncommented
    # test.uuid = default_uuid()
    s.save(test)
    s.flush()
    print 'Post-Flush:', test.uuid

    u = test_table.select().execute().fetchone()
    print 'DB:', u

          - - - - - - - - - - - - - - - - - - - - - - - - -


When run as is above, the output for me looks like:

Post-Flush: 1
DB: (u'1cfcb156-2a90-42ec-9c96-75a4b8bf60e7',)

(If you enable tracing on the engine, you can see that the actual uuid
 column value inserted into the database during the flush is, in fact,
 the data shown from the select, which doesn't match that in the object
 instance in memory following the flush)

Running with the commented line (manual key assignment) uncommented, yields:

Post-Flush: d05ebdde-267f-43ae-a7df-f6d588e431a2
DB: (u'd05ebdde-267f-43ae-a7df-f6d588e431a2',)

which is what I originally expected for the first case.

Now, I know that sqlite is internally creating a rowid field since I
don't have an Integer primary key, and I'm presuming that's what the
erroneous 1 value is in the first case (it is, in fact, an 'int', not
a string), but I don't know why it's being assigned to my in-memory
copy of the object, and since that's the primary key field, it no
longer maps properly to the database.

Am I doing something wrong in the above?

Thanks.

-- David


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to