Hi All,

We have a lot of declarative-mapped classes that have a method that looks like:

    @classmethod
    def find_or_create(cls, session, name):
        result = session.query(cls).filter_by(name=name).first()
        if result is None:
            result = cls(name=name)
            session.add(result)
        return result

This feels clunky to me:

1 - duplicate code on each class (there are a lot of classes!)

2 - feels like something that SA might provide itself

So why not use merge? Well, 'cos name isn't the primary key, an integer field called 'id' is. Can merge be made to work when it needs to look up the object by a key other than the primary?

OK, so why not put it in a base class? 'cos Declarative doesn't like base classes that don't map to a table, and writing a metaclass for this feels heinous...

Ideas gratefully received...

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk
-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
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