python - For my self referencing table query it can't compare a collection to an object or collection; -


class place(db.model):     __tablename__ = 'places'     id = db.column(db.integer, primary_key=true)     name = db.column(db.string)      lat = db.column(db.float, default=none)     long = db.column(db.float, default=none)       parent_place_id = db.column(db.integer, db.foreignkey('places.id'))     parent_place = db.relationship("place")      branches = db.relationship("branch", back_populates="place")       def __str__(self):         return self.name 

when tried following:

p_place = models.place.query.get(parent_place_id) if parent_place_id else none  places = models.place.query.filter(models.place.parent_place==p_place ).all() 

i following error:

traceback (most recent call last):   ...    file "./app/ecom_chatbot/ecom_conversation.py", line 127, in show_locations     places = models.place.query.filter(models.place.parent_place==p_place ).all()   file "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/sql/operators.py", line 304, in __eq__     return self.operate(eq, other)   file "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 175, in operate     return op(self.comparator, *other, **kwargs)   file "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1037, in __eq__     "can't compare collection object or collection; " sqlalchemy.exc.invalidrequesterror: can't compare collection object or collection; use contains() test membership. 

your parent_place relationship wrong. need many-to-one relationship.

see adjacency list relationships

the relationship() configuration here works in same way “normal” one-to-many relationship, exception “direction”, i.e. whether relationship one-to-many or many-to-one, assumed default one-to-many. establish relationship many-to-one, directive added known remote_side, column or collection of column objects indicate should considered “remote”:

you should declare parent_place relationship such:

parent_place = db.relationship("place", remote_side=[id])

where above, id column applied remote_side of parent relationship(), establishing parent_id “local” side, , relationship behaves many-to-one.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -