Michael Bayer wrote:

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

seems like a function you'd only need once, on a base class or similar.

OK, so the problems I had before with base classes were because I tried to have columns on my base classes?

2 - feels like something that SA might provide itself

does SQLA assume that all mapped objects have a field called "name" and are retrieved using a filter on a single column ?

Well, I was thinking a little more abstractly than that ;-)

The "general" purpose version of such a function would require writing out the whole query in a callable anyway so it doesn't make things any simpler than just using python.

I suppose, I guess I was hoping for something that took a query-that-could-only-return-one-object and returned either that object or a new one of the right type. Of course, I can see how that would be an unreasonable request ;-)

find_or_create() is also not very performant in the case where you're better off loading a whole set of rows first, so not a default the core needs to be recommending.

True, but if you were using it, you'd likely only be wanting one row at a time...

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...

declarative_base accepts an existing class, which you can apply whatever lookups and such before turning it into a declarative base.

I'm not sure I ever found an answer to my related question: is it okay to have a load of declarative_base classes floating around? I guess it shouldn't matter provided I get them all using the same metadata object. How would I do that?

Writing a metaclass is also a good way go, for the purposes of extending declarative to do what you want, not provide the actual find_or_create, which you still can put on a subclass.

Application developers should never be writing metaclasses! ;-)
(framework developers fair enough, but you guys have bigger and more careful brains...)

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