Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlAlchemy , AttributeError: 'tuple' object has no attribute 'foreign_keys'

I have the following models to describe my database schema:

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)

and when I try to create a Team like:

team = Team()

I get the following traceback after the command above:

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'

what I am doing wrong ?

like image 853
gosom Avatar asked Aug 12 '12 12:08

gosom


1 Answers

backref should be a keyword argument in your declaration of Competition.teams:

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

See the documentation on relationship. You can use a backref callable to configure the back reference explicitly, but you'd have to still use the backref keyword:

class Competition(Base):
    # ...
    teams = relationship('Team', backref=backref("competition", ... additional keywords ...))
like image 135
Martijn Pieters Avatar answered Oct 26 '22 03:10

Martijn Pieters