On Aug 27, 2014, at 3:38 PM, Bala Ramakrishnan <bal...@gmail.com> wrote:

> Thanks for the reply. I had implemented this feature about two months ago, 
> and I had forgotten myself. I had originally used this:
> 
> def rand_hex_str():
>     return '%016x' % random.getrandbits(64)
> 
> # using event listener to force foreign key definitions to use 'alter 
> statements'
> def fk_constraint_name(const, table):
>      fk_name = 'fk_' + rand_hex_str()  # generates a foreign key constraint 
> like: fk_ABC0234DEF
>      const.name = fk_name
>      const.use_alter = True    # generate alter statements.
> event.listen(ForeignKeyConstraint,  "before_parent_attach", 
> fk_constraint_name)
> 
> 
> Later, I replaced the sqlalchemy.ForiegnKey class with this wrapper function 
> before the model definitions start using foreign key relationships:
> 
> orig_fkey_class = sqlalchemy.ForeignKey
> def use_alter_fkey_func(*args, **kwargs):
>     if 'name' not in kwargs:
>         kwargs['name'] = 'fk_' + rand_hex_str()
>     if 'use_alter' not in kwargs:
>         kwargs['use_alter'] = True
>     return orig_fkey_class(*args, **kwargs)
> sqlalchemy.ForeignKey = use_alter_fkey_func
> 
> 
> Is there a drawback to replacing the sqlAlchemy ForeignKey class with this 
> wrapper function instead of using an event?

the wrapper function just means you have to make sure you use it everywhere you 
need.  But it does have the advantage that it is localized to the code that 
needs it, whereas the event hook is global on the ForeignKey class.




> 
> 
> 
> 
> 
> 
> On Wednesday, August 27, 2014 5:20:35 AM UTC-7, Michael Bayer wrote:
> you can alter how MetaData things are built up using the after_parent_attach 
> event:
> 
> http://docs.sqlalchemy.org/en/rel_0_9/core/events.html?highlight=after_parent_attach#sqlalchemy.events.DDLEvents.after_parent_attach
> 
> 
> apply the event to ForeignKey and set use_alter=True for all of them as they 
> come in.
> 
> 
> On Aug 26, 2014, at 10:03 PM, Bala Ramakrishnan <bal...@gmail.com> wrote:
> 
>> To generate an 'alter table ' statement for foreign key constraints, we have 
>> to specify use_alter=True for the ForeignKey class.
>> 
>> Is there a global setting or via setting up some kind of event call back to 
>> generate the alter statements by default for all foreign key constraints 
>> instead of setting them for each relationship?
>> 
>> I am trying to find a comparable setting with Java Hibernate.
>> 
>> -Bala
>> 
>> -- 
>> 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.
>> To post to this group, send email to sqlal...@googlegroups.com.
>> Visit this group at http://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 http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to