On 03/14/2016 11:15 AM, 'Chris Norman' via sqlalchemy wrote:
Hi all,
I've tried googling for this, and I get nothing. I have a table to store
data about objects. Each object should have a location property which
links back to the same table. Conversely, each object should have a
contents property which shows all objects which have their location set
to this object.

documentation for multiple relationships to the same table is at:

http://docs.sqlalchemy.org/en/rel_1_0/orm/join_conditions.html#handling-multiple-join-paths

hope this helps




This is the code I have so far:

class DBObject(Base):
  __tablename__ = 'db_objects'
  id = Column(Integer, primary_key = True)
  name = Column(String)
  description = Column(String)
  location_id = Column(Integer, ForeignKey('db_objects.id'))
  owner_id = Column(Integer, ForeignKey('db_objects.id'))
  contents = relationship('DBObject', remote_side = location_id, backref
= backref('location', remote_side = [location_id]))
  owned_objects = relationship('DBObject', remote_side = owner_id,
backref = backref('owner', remote_side = [owner_id]))
  x = Column(Float)
  y = Column(Float)
  z = Column(Float)
  max_hp = Column(Float)
  damage = Column(Float)
  properties = Column(LargeBinary)

When I issue Base.metadata.create_all I get this:

Traceback (most recent call last):
   File "C:\python35\lib\site-packages\sqlalchemy\orm\relationships.py",
line 2055, in _determine_joins
     consider_as_foreign_keys=consider_as_foreign_keys
   File "<string>", line 2, in join_condition
   File "C:\python35\lib\site-packages\sqlalchemy\sql\selectable.py",
line 828, in _join_condition
     a, b, constraints, consider_as_foreign_keys)
   File "C:\python35\lib\site-packages\sqlalchemy\sql\selectable.py",
line 918, in _joincond_trim_constraints
     "join explicitly." % (a.description, b.description))
sqlalchemy.exc.AmbiguousForeignKeysError: Can't determine join between
'db_objects' and 'db_objects'; tables have more than one foreign key
constraint relationship between them. Please specify the 'onclause' of
this join explicitly.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
   File "main.py", line 30, in <module>
     start()
   File "C:\Users\Chrisn Norman\Dropbox\SRC\mindspace_server\server.py",
line 15, in start
     db.initialise()
   File "C:\Users\Chrisn Norman\Dropbox\SRC\mindspace_server\db.py",
line 57, in initialise
     for row in session.query(DBObject):
   File "C:\python35\lib\site-packages\sqlalchemy\orm\session.py", line
1272, in query
     return self._query_cls(entities, self, **kwargs)
   File "C:\python35\lib\site-packages\sqlalchemy\orm\query.py", line
110, in __init__
     self._set_entities(entities)
   File "C:\python35\lib\site-packages\sqlalchemy\orm\query.py", line
120, in _set_entities
     self._set_entity_selectables(self._entities)
   File "C:\python35\lib\site-packages\sqlalchemy\orm\query.py", line
150, in _set_entity_selectables
     ent.setup_entity(*d[entity])
   File "C:\python35\lib\site-packages\sqlalchemy\orm\query.py", line
3421, in setup_entity
     self._with_polymorphic = ext_info.with_polymorphic_mappers
   File "C:\python35\lib\site-packages\sqlalchemy\util\langhelpers.py",
line 747, in __get__
     obj.__dict__[self.__name__] = result = self.fget(obj)
   File "C:\python35\lib\site-packages\sqlalchemy\orm\mapper.py", line
1893, in _with_polymorphic_mappers
     configure_mappers()
   File "C:\python35\lib\site-packages\sqlalchemy\orm\mapper.py", line
2768, in configure_mappers
     mapper._post_configure_properties()
   File "C:\python35\lib\site-packages\sqlalchemy\orm\mapper.py", line
1710, in _post_configure_properties
     prop.init()
   File "C:\python35\lib\site-packages\sqlalchemy\orm\interfaces.py",
line 183, in init
     self.do_init()
   File "C:\python35\lib\site-packages\sqlalchemy\orm\relationships.py",
line 1629, in do_init
     self._setup_join_conditions()
   File "C:\python35\lib\site-packages\sqlalchemy\orm\relationships.py",
line 1704, in _setup_join_conditions
     can_be_synced_fn=self._columns_are_mapped
   File "C:\python35\lib\site-packages\sqlalchemy\orm\relationships.py",
line 1972, in __init__
     self._determine_joins()
   File "C:\python35\lib\site-packages\sqlalchemy\orm\relationships.py",
line 2099, in _determine_joins
     % self.prop)
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join
condition between parent/child tables on relationship DBObject.contents
- there are multiple foreign key paths linking the tables.  Specify the
'foreign_keys' argument, providing a list of those columns which should
be counted as containing a foreign key reference to the parent table.


I'm using latest sqlalchemy from pip and Python 3.5.

Any ideas on how to fix this?

Cheers,

--
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 [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to