I would qualify this as a bug since the compiler should be executing the
guid() function externally to the INSERT statement, since its newly
generated value is required as a return value - this is ticket 1335.

Note that pre-execution of the guid function is required here.   You can
force this manually as follows:

Column('id', types.CHAR(36), primary_key=True,
         default=lambda ctx:
ctx.connection.scalar(func.convert(literal_column('UUID() USING
utf8'))))



Bryan wrote:
>
> The primary keys in my db are GUIDs, char(36).  When I generate the
> GUID in python using the uuid module, everything works fine.  But when
> I allow the db to generate the GUIDs I get foreign key errors when
> trying to save a new parent and child.
>
> A look at the SQL generated shows that the parent is being saved
> first, but when the child is saved, it does not have the parent's new
> primary key in the related field.  Instead of the parent's new GUID in
> the related field, it has 0L.
>
> When using the first method below, what is stopping sqlalchemy from
> getting the newly created guid so it can be referenced by the child's
> SQL??
>
> # This way does not work
> #
> -----------------------------------------------------------------------
> def colId(): return Column('id', types.CHAR(36), primary_key=True,
>         default=func.convert(literal_column('UUID() USING utf8')))
>
> # This way works
> #
> -----------------------------------------------------------------------
> from uuid import uuid4
> def colId(): return Column('id', types.CHAR(36), primary_key=True,
>         default=lambda: str(uuid4()))
>
> >
>


--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to