On 31 Jul 2014, at 21:34, Rick Otten <rottenwindf...@gmail.com> wrote:

> I'm using sqlalchemy 0.8.7 on python 2.7.8.
> 
> Here is my test case:
> 
> #!/usr/bin/env python
> 
> from sqlalchemy import Column, ForeignKey
> from sqlalchemy.orm import relationship, backref
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.dialects.postgresql import *
> 
> import uuid
> 
> Base = declarative_base()
> 
> class parent(Base):
> 
>    __tablename__ = 'parent'
> 
>    parent_id    = Column("parent_id", UUID(as_uuid=True), primary_key=True)
>    description  = Column("description", VARCHAR)
> 
>    childRelationship = relationship('child', backref = "parent")
> 
> #Base2 = declarative_base()
> 
> #class child(Base2):
> class child(Base):
> 
>    __tablename__ = 'child'
> 
>    child_id      = Column("child_id", UUID(as_uuid=True), primary_key=True)
>    description   = Column("description", VARCHAR)
>    parent_id     = Column("parent_id", UUID(as_uuid=True), ForeignKey(parent))
> 
> 
> myP = parent()
> 

That ForeignKey definition looks wrong - it should point at a column, not a 
class. I think you want:

  ForeignKey(parent.parent_id)


> 
> From the documentation I thought I'd be able to instantiate "myP" at that 
> point.
> Instead I get this:
> 
> Traceback (most recent call last):
>   File "./testcase.py", line 33, in <module>
>     myP = parent()
>   File "<string>", line 2, in __init__
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/instrumentation.py", line 
> 317, in _new_state_if_none
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/util/langhelpers.py", line 
> 612, in __get__
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/instrumentation.py", line 
> 152, in _state_constructor
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/event.py", line 420, in 
> __call__
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/mapper.py", line 2262, in 
> _event_on_first_init
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/mapper.py", line 2171, in 
> configure_mappers
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/mapper.py", line 1281, in 
> _post_configure_properties
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/interfaces.py", line 231, 
> in init
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/properties.py", line 
> 1030, in do_init
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/properties.py", line 
> 1104, in _setup_join_conditions
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/relationships.py", line 
> 114, in __init__
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/relationships.py", line 
> 197, in _determine_joins
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/util.py", line 355, in 
> join_condition
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/schema.py", line 1406, in 
> get_referent
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/expression.py", line 
> 3363, in corresponding_column
> AttributeError: type object 'parent' has no attribute 'proxy_set'
> 
> If I change the child class to use "Base2" instead of "Base" (commented out 
> above), it changes the error:
> 
> Traceback (most recent call last):
>   File "./testcase.py", line 33, in <module>
>     myP = parent()
>   File "<string>", line 2, in __init__
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/instrumentation.py", line 
> 317, in _new_state_if_none
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/util/langhelpers.py", line 
> 612, in __get__
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/instrumentation.py", line 
> 152, in _state_constructor
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/event.py", line 420, in 
> __call__
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/mapper.py", line 2262, in 
> _event_on_first_init
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/mapper.py", line 2171, in 
> configure_mappers
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/mapper.py", line 1281, in 
> _post_configure_properties
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/interfaces.py", line 231, 
> in init
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/properties.py", line 
> 1029, in do_init
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/properties.py", line 
> 1084, in _process_dependent_arguments
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/util/langhelpers.py", line 
> 612, in __get__
>   File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/properties.py", line 
> 1003, in mapper
>   File 
> "build/bdist.linux-x86_64/egg/sqlalchemy/ext/declarative/clsregistry.py", 
> line 266, in return_cls
> sqlalchemy.exc.InvalidRequestError: When initializing mapper 
> Mapper|parent|parent, expression 'child' failed to locate a name ("name 
> 'child' is not defined"). If this is a class name, consider adding this 
> relationship() to the <class '__main__.parent'> class after both dependent 
> classes have been defined.

This is expected. The call to declarative_base() creates a registry for classes 
that inherit from that Base. When you use strings instead of classes in your 
relationships (as you do when creating childRelationship), those strings are 
looked up in the registry to find the class that they refer to. Because 'child' 
and 'parent' exist in different registries, sqlalchemy can't resolve the string.

Hope that helps,

Simon

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