Hi all,

The way single table inheritance works seem a bit "unclean" to me: all
columns are available as properties in all classes. Even if they map
to the same table, ideally each object should only have a subset of
columns available, right? SQLAlchemy can't guess which object has
which columns, so I naturally tried to map each class to a select
including only their respective columns. But I couldn't get that to
work. So I tried without inheritance and couldn't get it to work
either (see attached file).

First, it complained there was no alias, while I *think* aliases
should only be necessary when the query is used in a subquery, but
anyway, after giving it its alias, it still doesn't work: I can't save
any data. Worse, it doesn't complain in any way, it's just that no
insert is issued to the database. I suspect a limitation in SQLAlchemy
(ie no inserts when there isn't at least one complete table in the
selectable or something of the like), but it might be something I'm
doing wrong. Any idea?

BTW: it's probably related to the fact the generated select query is:

SELECT subset.id AS subset_id, subset.data AS subset_data
FROM (SELECT common.id AS id, common.data AS data FROM common) AS
subset ORDER BY subset.oid

while I'd like to be able to do is simply:

SELECT common.id AS common_id, common.data AS common_data
FROM common
ORDER BY subset.oid

-- 
Gaƫtan de Menten
http://openhex.org

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

from sqlalchemy  import *
from sqlalchemy.orm  import *

metadata = MetaData('sqlite:///')
metadata.bind.echo = True

common_table = Table('common', metadata,
    Column('id', Integer, primary_key=True),
    Column('data', Integer),
    Column('extra', String(45)),
)


class Subset(object):
    pass

subset_select = select([common_table.c.id, common_table.c.data]).alias('subset')
subset_mapper = mapper(Subset, subset_select)

metadata.create_all()

#common_table.insert().execute(subset_no=1)
#subset_select.insert().execute(subset_no=1)
#print [dict(e) for e in subset_select.execute().fetchall()]

sess = create_session()
l = Subset()
l.data = 1
sess.save(l)
sess.flush()
sess.clear()

print sess.query(Subset).list()

Reply via email to