If you want a collection of scalar values, use traditional mappings and relationship() in combination with association_proxy.  See http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/associationproxy.html#simplifying-scalar-collections .re: "or dictionaries".  Use attribute_mapped_collection:  http://docs.sqlalchemy.org/en/rel_0_7/orm/collections.html#dictionary-collections"dictionaries plus scalars": combine both attribute_mapped_collection and association_proxy: http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/associationproxy.html#proxying-to-dictionary-based-collections
Is the use case of having nested collections really all that out of mainstream?
 It seems like a fairly straightforward requirement, though perhaps with a lot of boilerplate.  A simple list-of-lists seems achievable using something like:all_lists = Table('all_lists', metadata,          Column('id', Integer, primary_key=True),          Column('type', String(32)),  # polymorphic type... one of 'list' or 'scalar'          Column('scalar_id', Integer, ForeignKey('scalars.id')),          Column('list_id', Integer, ForeignKey('all_lists.id')))with an appropriate polymorphic identifier.  Am I going down a dead-end here?Â
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
On Aug 27, 2012, at 4:43 PM, Jacob Biesinger wrote:Is the use case of having nested collections really all that out of mainstream?I would think just about any non-trivial application winds up having "nested collections". Â Â In relational databases this just means you have one-to-many relationships chained along. Â relationship() is the SQLAlchemy mechanism to achieve one-to-many. Â Â This is like the most prominent feature in the whole ORM so I'm not sure what's causing it to appear "non mainstream".
If you're getting at the idea of having two or three big tables that generically handle all kinds of lists and dictionaries from all over the rest of the schema, yes, I'd characterize it as common, but not necessarily a best practice in most cases. Â It doesn't scale very well as those two or three giant tables grow to be huge, a single point of contention, hard to index and manipulate. Â Nevertheless we support this architecture through such notions as the "polymorphic association", which you'll find in the examples with the distribution. Â Â
On Mon, Aug 27, 2012 at 4:57 PM, Michael Bayer <mik...@zzzcomputing.com> wrote:On Aug 27, 2012, at 4:43 PM, Jacob Biesinger wrote:Is the use case of having nested collections really all that out of mainstream?I would think just about any non-trivial application winds up having "nested collections". Â Â In relational databases this just means you have one-to-many relationships chained along. Â relationship() is the SQLAlchemy mechanism to achieve one-to-many. Â Â This is like the most prominent feature in the whole ORM so I'm not sure what's causing it to appear "non mainstream".If you're getting at the idea of having two or three big tables that generically handle all kinds of lists and dictionaries from all over the rest of the schema, yes, I'd characterize it as common, but not necessarily a best practice in most cases. Â It doesn't scale very well as those two or three giant tables grow to be huge, a single point of contention, hard to index and manipulate. Â Nevertheless we support this architecture through such notions as the "polymorphic association", which you'll find in the examples with the distribution. Â ÂYes, this is the pattern I'm trying to describe though I've been thinking of these as "typed collections", smallish tables for handling a particular table's collections needs, which are all of a single type (so rows are either a collection or a FK to ONE other table). Â It feels very doable, given the many examples I've seen that don't go quite this far. Â If it's a common enough use case, why not have an example on the best way to do this?Â
Yes, this is the pattern I'm trying to describe though I've been thinking of these as "typed collections", smallish tables for handling a particular table's collections needs, which are all of a single type (so rows are either a collection or a FK to ONE other table). Â It feels very doable, given the many examples I've seen that don't go quite this far. Â If it's a common enough use case, why not have an example on the best way to do this?ÂUh, scroll up, I said, "which you'll find in the **examples** with the distribution". Â Â start reading here:Â http://docs.sqlalchemy.org/en/rel_0_7/orm/examples.html#examples-generic-associations
Well, you dug into a really esoteric and complex example there. Â Â association_proxy is a much easier way to get around these cases where you want an object to act like a scalar, so here's that, without all that crazy boilerplate of the "vertical" example, which I'd avoid as it is really too complex.
 Your example seemed undecided about primary key style so I went with the composite version.   Surrogate + composite can't be mixed in a single table (well it can, but its relationally incorrect.  The key should be the smallest unit that identifies a row - http://en.wikipedia.org/wiki/Unique_key is a good top level read into various subjects regarding this).