10

我有以下模型来描述我的数据库架构:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, backref
import sqlalchemy.dialects.mysql as mysql

Base = declarative_base()

class Country(Base):
    __tablename__ = 'countries'
    __table_args__ = {  
        'mysql_engine': 'InnoDB',  
        'mysql_charset': 'utf8'  
    }   

    id = Column(mysql.TINYINT(unsigned=True), primary_key=True)
    name = Column(mysql.VARCHAR(30), nullable=False)
    competitions = relationship('Competition', backref='country')


class Competition(Base):
    __tablename__ = 'competitions'
    __table_args__ = {  
        'mysql_engine': 'InnoDB',  
        'mysql_charset': 'utf8'  
    }   

    id = Column(mysql.INTEGER(unsigned=True), primary_key=True)
    name = Column(mysql.VARCHAR(30), nullable=False)
    country_id = Column(mysql.TINYINT(unsigned=True), ForeignKey('countries.id'))
    teams = relationship('Team', backref("competition") )


class Team(Base):
    __tablename__ = 'teams'
    __table_args__ = {  
        'mysql_engine': 'InnoDB',  
        'mysql_charset': 'utf8'  
    }   

    id = Column(mysql.INTEGER(unsigned=True), primary_key=True)
    name = Column(mysql.VARCHAR(30), nullable=False)
    competition_id = Column(mysql.INTEGER(unsigned=True), ForeignKey('competitions.id'), nullable=False)

当我尝试创建一个团队时:

team = Team()

在上面的命令之后,我得到以下回溯:

Traceback (most recent call last):
  File "/home/giorgos/apps/Aptana_Studio_3/plugins/org.python.pydev_2.6.0.2012062121/pysrc/pydevd.py", line 1392, in <module>
    debugger.run(setup['file'], None, None)
  File "/home/giorgos/apps/Aptana_Studio_3/plugins/org.python.pydev_2.6.0.2012062121/pysrc/pydevd.py", line 1085, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "/home/giorgos/Documents/Aptana Studio 3 Workspace/BetPick/tests/insert_models.py", line 21, in <module>
    team = Team()
  File "<string>", line 2, in __init__
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py", line 309, in _new_state_if_none
    state = self._state_constructor(instance, self)
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 485, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py", line 157, in _state_constructor
    self.dispatch.first_init(self, self.class_)
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/event.py", line 291, in __call__
    fn(*args, **kw)
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2342, in _event_on_first_init
    configure_mappers()
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2258, in configure_mappers
    mapper._post_configure_properties()
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 1167, in _post_configure_properties
    prop.init()
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 128, in init
    self.do_init()
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/properties.py", line 911, in do_init
    self._determine_joins()
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/properties.py", line 1034, in _determine_joins
    self.secondary)
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/properties.py", line 1028, in _search_for_join
    a_subset=mapper.local_table)
  File "/home/giorgos/.virtualenvs/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/util.py", line 262, in join_condition
    b.foreign_keys, 
AttributeError: 'tuple' object has no attribute 'foreign_keys'

我做错了什么?

4

1 回答 1

16

backref应该是您声明中的关键字参数Competition.teams

class Competition(Base):
    # ...
    teams = relationship('Team', backref="competition")

请参阅 上的文档relationship。您可以使用backref可调用来显式配置反向引用,但您仍必须使用backref关键字:

class Competition(Base):
    # ...
    teams = relationship('Team', backref=backref("competition", ... additional keywords ...))
于 2012-08-12T12:29:57.220 回答