sqlalchemy - SqlSoup relate() for many-to-many relation throwing exception -
i'm using following code use sqlsoup existing database.
import sqlalchemy sqlalchemy.ext.sqlsoup import sqlsoup sqlalchemy.orm import backref engine = sqlalchemy.create_engine('postgresql:///test') db = sqlsoup(engine) db.books.relate('author', db.authors) db.books.relate('tags', db.tags, secondary=db.tags2books, backref=backref('books', lazy=false))
however, last relate()
call throws exception:
traceback (most recent call last): file "sqlsoup-test.py", line 10, in <module> db.books.relate('tags', db.tags, secondary=db.tags2books, backref=backref('books', lazy=false)) file "/usr/lib64/python2.6/site-packages/sqlalchemy/ext/sqlsoup.py", line 384, in relate class_mapper(cls)._configure_property(propname, relationship(*args, **kwargs)) file "/usr/lib64/python2.6/site-packages/sqlalchemy/orm/mapper.py", line 758, in _configure_property prop.init() file "/usr/lib64/python2.6/site-packages/sqlalchemy/orm/interfaces.py", line 476, in init self.do_init() file "/usr/lib64/python2.6/site-packages/sqlalchemy/orm/properties.py", line 895, in do_init self._determine_joins() file "/usr/lib64/python2.6/site-packages/sqlalchemy/orm/properties.py", line 1010, in _determine_joins self.secondary) file "/usr/lib64/python2.6/site-packages/sqlalchemy/orm/properties.py", line 1002, in _search_for_join return join_condition(mapper.local_table, table) file "/usr/lib64/python2.6/site-packages/sqlalchemy/sql/util.py", line 219, in join_condition fk in b.foreign_keys: attributeerror: 'mappedtags2books' object has no attribute 'foreign_keys'
the tables have been created using following definition:
authors_table = table('authors', metadata, column('id', integer, primary_key=true), column('name', string, nullable=false, index=true) ) books_table = table('books', metadata, column('id', integer, primary_key=true), column('author_id', integer, foreignkey('authors.id', onupdate='cascade', ondelete='cascade'), nullable=false), column('title', string, nullable=false, index=true) ) tags_table = table('tags', metadata, column('id', integer, primary_key=true), column('tag', string, index=true, unique=true, nullable=false) ) tags2books_table = table('tags2books', metadata, column('tag_id', integer, foreignkey('tags.id', onupdate='cascade', ondelete='cascade'), primary_key=true), column('book_id', integer, foreignkey('books.id', onupdate='cascade', ondelete='cascade'), primary_key=true) )
in case going ask "why using sqlsoup when can use real sqlalchemy?": i'm writing term paper sqlalchemy , i'm mentioning sqlsoup in 1 chapter , i'd add example. , what's easier using database i've used sa examples...
looks secondary
can't mapped class. passing table fixes error:
db.books.relate('tags', db.tags, secondary=db.tags2books._table, backref=backref('books', lazy=false))
Comments
Post a Comment