On Nov 10, 2011, at 9:34 AM, Mark Erbaugh wrote:

> I'm trying to use data from a sequence to add columns to a SQLAlchemy table 
> created declaratively. Here's what I'm doing:
> 
> class Sizing(Base):
>    __tablename__ = 'sizing'
>    id = Column(Integer, primary_key=True)
> 
>    [...]
> 
> 
> for name in ('column1', 'column2', 'column3', ...):
>    x = Column(type_=Integer)
>    x.name = name
>    Sizing.__table__.append_column(x)


that will just add the column to the table but won't map it.   the mapper isn't 
aware of this operation.

> 
> This works as far as creating the table in the database, i.e. viewing the 
> database shows columns named column1, column2, column3, etc, but code like 
> getattr(sizing, 'column1')  (sizing is an instance of Sizing) is failinging 
> with a message "'Sizing' object has no attribute 'column1'"
> 
> While code like:
> 
> Sizing.colum1 = Column(Integer)
> 
> works, but

right so that hits the __setattr__ of the DeclarativeMeta class which receives 
the Column object, checks it out, and assigns it correctly to the mapper and 
table.

> 
> Sizing.__dict__['column1'] = Column(Integer)

In general, you should never set attributes this way from the outside, that's 
just a Python thing, as you're bypassing whatever attribute set mechanics may 
be present on the target object.

> or
> 
> Sizing.__setattr__(Sizing, 'column1', Column(Integer))

this is not much different as again you're bypassing instrumentation that may 
be available on the class.   Use the Python setattr() function instead:  
setattr(Sizing, "name", object).


-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to