I'm using the following code to use SqlSoup with an existing database.
import sqlalchemy
from sqlalchemy.ext.sqlsoup import SqlSoup
from 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, the last relate()
call throws an 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
for fk in b.foreign_keys:
AttributeError: 'MappedTags2books' object has no attribute 'foreign_keys'
The tables have been created using the 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='CASC开发者_开发技巧ADE', 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 you are going to ask "Why is he using SqlSoup when he can use the real SQLAlchemy?": I'm writing a term paper about SQLAlchemy and I'm mentioning SqlSoup in one chapter and I'd like to add an example. And what's easier than using the database I've used for the SA examples...
Looks like secondary
can't be a mapped class. Passing a table fixes the error:
db.books.relate('tags', db.tags, secondary=db.tags2books._table, backref=backref('books', lazy=False))
精彩评论