On Jan 18, 2012, at 8:44 AM, Ronan Dunklau wrote:

> Hello.
> 
> I'm having trouble mapping a relationship on a column_property.
> 
> A similar problem has been reported in an old post on this mailing list,
> but with no answers:
> http://groups.google.com/group/sqlalchemy/browse_thread/thread/f7a13d9ca1494060/6fe890bb49c8dfa8#6fe890bb49c8dfa8
> 
> I have a column containing a code, a substring of this code refers to
> another table's primary key.
> 
> I tried the following solutions, but none works:
> 
> 1) "Naive" joining on the attributes:
> 
> Table1.rel_to_table2 = relationship(Table2,
>       viewonly=True,
>       uselist=False,
>       primaryjoin=func.substring(Table1.code, 1, 3) == Table2.code)
> 
> 2) Defining an intermediate column property:
> 
> Table1.table2_code = column_property(func.substring(Table1.col1, 1, 3))
> 
> Table1.rel_to_table2 = relationship(Table2,
>        viewonly=True,
>        uselist=False,
>       primartyjoin=Table1.table2_code == Table2.code)
> 
> 3) Adding a foreign_keys argument to the relationship, on the actual columns
> 
> This results in an error:
> 
> sqlalchemy/orm/properties.py", line 1458, in _refers_to_parent_table
>    pt.is_derived_from(c.table) and \
> AttributeError: 'Function' object has no attribute 'table'


this is an advanced form of mapping that's not documented right now, as it's 
been quasi-experimental for some time.      

You wouldn't need column_property here, instead you do it like this ("table1" 
and "table2" should be Table objects, not the classes, like Table2.__table__):

rel = relationship(Table2, 
                              viewonly=True, 
                              uselist=False, 
                             _local_remote_pairs=[(table1.c.code, 
table2.c.code)], 
                             primaryjoin=func.substring(table1.c.code, 1, 3) == 
table2.c.code, 
                             foreign_keys=[table1.c.code]
                           )

I'm not sure which side here you'd want to consider "foreign", that is, the 
column which seeks to match an existing column.   Switching "foreign_keys" to 
point to table1.c.code or table2.c.code will cause relationship to consider it 
as many-to-one, or one-to-many, respectively.




> 
> Thank you!
> 
> 
> --
> Ronan Dunklau
> 
> -- 
> 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.
> 

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