heres a patch for that:
Index: lib/sqlalchemy/ext/declarative.py
===================================================================
--- lib/sqlalchemy/ext/declarative.py (revision 5902)
+++ lib/sqlalchemy/ext/declarative.py (working copy)
@@ -552,6 +552,12 @@
_undefer_column_name(key, value)
cls.__table__.append_column(value)
cls.__mapper__.add_property(key, value)
+ elif isinstance(value, ColumnProperty):
+ for col in value.columns:
+ if isinstance(col, Column) and col.table is None:
+ _undefer_column_name(key, col)
+ cls.__table__.append_column(col)
+ cls.__mapper__.add_property(key, value)
elif isinstance(value, MapperProperty):
cls.__mapper__.add_property(key, _deferred_relation(cls,
value))
else:
ive added #1379 for this, someone needs to add some tests and commit.
scott wrote:
>
> Hi-
>
> I'm trying to add a deferred column to a declarative class after the
> class has been created (but before tables are created, obviously).
> This works fine with none-deferred columns, as documented on
> declarative.py:48, but deferred columns added in this way don't get
> added to the table definition in SQL.
>
> Here's how I'm adding the attribute:
>
> Bar.deferme = deferred(Column('deferme', String(30)))
>
> I've also tried without the column name--that fails in a different
> way, but still doesn't add the column definition.
>
> A full example demonstrating this is below. Is there any workaround
> for this?
>
> Thanks!
> Scott
>
> ****
>
> """
> example of (bug?) in adding deferred columns to a declarative class
> after the
> class's creation.
>
> Foo is a normal declarative class with a deferred column. Bar is the
> same class,
> but with the deferred column added afterwards.
> """
>
> from sqlalchemy import *
> from sqlalchemy.orm import *
> from sqlalchemy.ext.declarative import declarative_base
>
> metadata = MetaData('sqlite://')
> metadata.bind.echo = True
> Base = declarative_base(metadata=metadata)
>
> class Foo(Base):
> __tablename__ = 'foos'
> id = Column(Integer(unsigned=True), primary_key=True)
> text = Column(String(20))
> deferme = deferred(Column(String(30)))
>
> def __init__(self, text, deferme):
> self.text = text
> self.deferme = deferme
>
> def __repr__(self):
> return "<Foo: id %d, text %s>" % (self.id, self.text)
>
> class Bar(Base):
> __tablename__ = 'bars'
> id = Column(Integer(unsigned=True), primary_key=True)
> text = Column(String(20))
>
> def __init__(self, text, deferme):
> self.text = text
> self.deferme = deferme
>
> def __repr__(self):
> return "<Bar: id %d, text %s>" % (self.id, self.text)
>
> Bar.deferme = deferred(Column('deferme', String(30)))
>
> # This doesn't work either:
> # Bar.deferme = deferred(Column(String(30)))
>
> # populate
> metadata.create_all()
> sess = create_session()
>
> foo1 = Foo('sqlalchemy', 'rocks')
> foo2 = Foo('but', 'I')
>
> bar1 = Bar('want', 'better')
> bar2 = Bar('deferred', 'columns')
>
> sess.add(foo1)
> sess.add(foo2)
>
> sess.add(bar1)
> sess.add(bar2)
>
> sess.flush()
> sess.clear()
>
> # query objects, get their addresses
> print "all foos"
> for f in sess.query(Foo).all():
> print f.text, f.deferme
>
> print "all bars"
> for b in sess.query(Bar).all():
> print b.text, b.deferme
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---