Unless I miss something, this much more simple code should work:

        n = 'a subject'
        hub.begin()
        try:
            rs = Subject.byName(n)
            print "Found: %s" % rs
        except SQLObjectNotFound:
            print "%s Not Found! Creating..." % rs
            rs = Subject(name=n)
        hub.commit()
        hub.end()

of course you must have alternateID=True in your model, i.e. something like

       name = StringCol(alternateID=True, length=200)


Olivier.


Soni Bergraj wrote:
> Hello,
> I try to get a Subject from the database, if this fails i want to create
> a new one. But how ever i arrange hub.begin(), hub.end() and
> hub.commit() i always get an
>
> AssertionError: This transaction has already gone through ROLLBACK;
> begin another transaction
>
> For the except statement (object does not exist, create a new one)
>
>         hub.begin()
>         try:
>             subject     = Subject.byName(name)
>             hub.commit
>             hub.end()
>
>         except SQLObjectNotFound:
>             hub.end()
>             hub.begin()
>             subject     = Subject(name = name)
>             hub.commit()
>             hub.end()
>
> The hub is of type PackageHub, I didn't found documentation for it
> (deprecated?).
>
> Thanks for help,
>
>   

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to