Awesome, both look good, will try them out. Thanks for the quick reply.

On Thursday, March 17, 2016 at 3:54:11 PM UTC-6, Mike Bayer wrote:
>
> Just core, you can some_table.append_column(Column(... whatever...)) at 
> any time.    The only "event" I can think of are the DDL attachment 
> events: 
>
> from sqlalchemy import * 
> from sqlalchemy import event 
>
> m = MetaData() 
>
>
> table_a = Table( 
>      'a', m, 
>      Column('id', Integer) 
> ) 
>
>
> @event.listens_for(Table, "after_parent_attach") 
> def evt(target, parent): 
>      if parent is m and target.name == 'b': 
>          m.tables['a'].append_column( 
>              Column('data', Integer, default=select([target]).as_scalar()) 
>          ) 
>
> table_b = Table( 
>      'b', m, 
>      Column('id', Integer) 
> ) 
>
> e = create_engine("sqlite://", echo=True) 
> m.create_all(e) 
>
> e.execute(table_a.insert()) 
>
>
> I guess if you really want to get the column default to be something 
> that becomes the SQL at the last minute, you could do something like this: 
>
> from sqlalchemy import * 
> from sqlalchemy.sql import ColumnElement 
> from sqlalchemy.ext.compiler import compiles 
>
>
> class MyThing(ColumnElement): 
>      pass 
>
>
> @compiles(MyThing) 
> def _generate_my_thing(element, compiler, **kw): 
>      return compiler.process(select([table_b]).as_scalar()) 
>
> m = MetaData() 
>
>
> table_a = Table( 
>      'a', m, 
>      Column('id', Integer), 
>      Column('data', Integer, default=MyThing()) 
> ) 
>
> table_b = Table( 
>      'b', m, 
>      Column('id', Integer) 
> ) 
>
> e = create_engine("sqlite://", echo=True) 
> m.create_all(e) 
>
> e.execute(table_a.insert()) 
>
>
>
>
>
> On 03/17/2016 05:25 PM, Jonathan Beluch wrote: 
> > We're just using core, is there some equivalent? 
> > 
> > On Thursday, March 17, 2016 at 3:19:23 PM UTC-6, Mike Bayer wrote: 
> > 
> > 
> > 
> >     On 03/17/2016 04:47 PM, Jonathan Beluch wrote: 
> >      > Background: Using core we have tables defined in a few separate 
> >     files. 
> >      > Goal: To have column defaults be selectables which reference 
> other 
> >      > tables while avoiding circular imports. To avoid circular imports 
> I 
> >      > cannot always build the selects at import time, they have to be 
> >      > generated inside a function that takes table/col names similar to 
> >     how 
> >      > FKs work. 
> >      > 
> >      > It seems that a callable for Column.default cannot return an 
> >     uncompiled 
> >      > statement. Two solutions I see: 
> >      > 
> >      > 1) Use the context provided to default callables to compile the 
> >     dynamic 
> >      > select statement. 
> >      > 2) Implement something similar to FKs, using all the parent 
> attach 
> >      > events to set .arg to a selectable on a subclass of 
> ColumnDefault. 
> >      > 
> >      > Thoughts? 
> > 
> >     A column default callable is expected to produce the value that's to 
> be 
> >     embedded into the INSERT values.   You can execute any SQL you'd 
> like 
> >     there, but that's after the INSERT statement's string form is 
> already 
> >     decided. 
> > 
> >     If the goal is that the default is a SQL clause to be embedded in 
> the 
> >     string form of the INSERT, then you use a fixed default that is 
> >     represented by that SQL clause. 
> > 
> >     If you can't generate that SQL clause due to imports, there's 
> various 
> >     ways to defer the production of the Column but the simplest way, not 
> >     necessarily the nicest, is to stick it in declare_first: 
> > 
> >     from sqlalchemy import * 
> >     from sqlalchemy.orm import * 
> >     from sqlalchemy.ext.declarative import declarative_base 
> > 
> >     Base = declarative_base() 
> > 
> > 
> >     class B(Base): 
> >           __tablename__ = 'b' 
> >           id = Column(Integer, primary_key=True) 
> > 
> >           @classmethod 
> >           def __declare_first__(cls): 
> >               cls.b = Column(Integer, default=select([A]).as_scalar()) 
> > 
> > 
> >     class A(Base): 
> >           __tablename__ = 'a' 
> >           id = Column(Integer, primary_key=True) 
> > 
> >     e = create_engine("sqlite://", echo=True) 
> >     configure_mappers() 
> >     Base.metadata.create_all(e) 
> > 
> >     s = Session(e) 
> >     s.add(B()) 
> >     s.commit() 
> > 
> > 
> >     __declare_first__ is actually just a hook for the before_configured 
> >     event so that's actually using the events in any case. 
> > 
> > 
> > 
> > 
> >      > 
> >      > -- 
> >      > You received this message because you are subscribed to the 
> Google 
> >      > Groups "sqlalchemy" group. 
> >      > To unsubscribe from this group and stop receiving emails from it, 
> >     send 
> >      > an email to sqlalchemy+...@googlegroups.com <javascript:> 
> >      > <mailto:sqlalchemy+unsubscr...@googlegroups.com <javascript:> 
> <javascript:>>. 
> >      > To post to this group, send email to sqlal...@googlegroups.com 
> >     <javascript:> 
> >      > <mailto:sqlal...@googlegroups.com <javascript:>>. 
> >      > Visit this group at https://groups.google.com/group/sqlalchemy 
> >     <https://groups.google.com/group/sqlalchemy>. 
> >      > For more options, visit https://groups.google.com/d/optout 
> >     <https://groups.google.com/d/optout>. 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> > Groups "sqlalchemy" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> > an email to sqlalchemy+...@googlegroups.com <javascript:> 
> > <mailto:sqlalchemy+unsubscr...@googlegroups.com <javascript:>>. 
> > To post to this group, send email to sqlal...@googlegroups.com 
> <javascript:> 
> > <mailto:sqlal...@googlegroups.com <javascript:>>. 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to