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:>>. 
> > 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