'tuple' object has no attribute 'foreign_keys'

977 views
Skip to first unread message

willian coelho

unread,
May 13, 2015, 11:49:43 AM5/13/15
to sqlal...@googlegroups.com
Hello everyone,

I`m getting an error when creating a new instance of my model `Type`. I have a many-to-many relationship between Type and Maker.

Code:
type_maker = db.Table(
   
'type_maker',
    db
.metadata,
    db
.Column('type_id', db.Integer, db.ForeignKey('type.id')),
    db
.Column('maker_id', db.Integer, db.ForeignKey('maker.id')))


class Type(db.Model):
   
"""VehicleType
    """
    __tablename__ = 'type'
    id = db.Column(db.Integer, primary_key=True)
    name
= db.Column(db.String(50), unique=True, index=True)
    makers
= db.relationship('Maker', secondary='type_maker', backref=db.backref('type'))

   
def __repr__(self):
       
return '<Type #{}: {}>'.format(self.id, self.name)


class Maker(db.Model):
   
"""VehicleMaker
    """
    __tablename__ = 'maker'
    id = db.Column(db.Integer, primary_key=True)
    name
= db.Column(db.String(50), index=True)
    versions
= db.relationship('Version', backref=db.backref('maker'))

   
def __repr__(self):
       
return '<Maker #{}: {}>'.format(self.id, self.name)

type_
= Type(name='Car')


Error:
'tuple' object has no attribute 'foreign_keys'

What is wrong with the code?

Thanks in advance!
Michael Coelho.

Mike Bayer

unread,
May 13, 2015, 1:08:38 PM5/13/15
to sqlal...@googlegroups.com


On 5/13/15 11:49 AM, willian coelho wrote:
Hello everyone,

I`m getting an error when creating a new instance of my model `Type`. I have a many-to-many relationship between Type and Maker.

Error:
'tuple' object has no attribute 'foreign_keys'

What is wrong with the code?
please check your code for errant commas at the end of a declaration or argument name.  would need to see a complete stack trace to narrow down what kind of object is being misinterpreted.





Thanks in advance!
Michael Coelho.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

willian coelho

unread,
May 13, 2015, 1:14:19 PM5/13/15
to sqlal...@googlegroups.com
Follow the complete Stack Trace:

Traceback (most recent call last):
  File "/Users/macbookair2011/Desktop/projects/portautos/manage.py", line 194, in <module>
    manager.run()
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/Users/macbookair2011/Desktop/projects/portautos/manage.py", line 41, in load_types_and_makers
    t = Type(name=type_)
  File "<string>", line 2, in __init__
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py", line 324, in _new_state_if_none
    state = self._state_constructor(instance, self)
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 725, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py", line 158, in _state_constructor
    self.dispatch.first_init(self, self.class_)
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/event/attr.py", line 260, in __call__
    fn(*args, **kw)
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2702, in _event_on_first_init
    configure_mappers()
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2598, in configure_mappers
    mapper._post_configure_properties()
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 1696, in _post_configure_properties
    prop.init()
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 144, in init
    self.do_init()
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1551, in do_init
    self._setup_join_conditions()
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1625, in _setup_join_conditions
    can_be_synced_fn=self._columns_are_mapped
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1893, in __init__
    self._determine_joins()
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1959, in _determine_joins
    consider_as_foreign_keys=consider_as_foreign_keys
  File "<string>", line 2, in join_condition
  File "/Users/macbookair2011/Documents/envs/portautosapp/lib/python2.7/site-packages/sqlalchemy/sql/selectable.py", line 707, in _join_condition
    b.foreign_keys,
AttributeError: 'tuple' object has no attribute 'foreign_keys'

Mike Bayer

unread,
May 13, 2015, 1:21:01 PM5/13/15
to sqlal...@googlegroups.com
there's a comma somewhere.  The code you have sent is incomplete.  There is no mapping "Version" for example.




willian coelho

unread,
May 13, 2015, 1:23:07 PM5/13/15
to sqlal...@googlegroups.com
Sorry Michael,

# coding:utf-8
from portautos.ext.db import db

# association between type and maker tables
type_maker = db.Table('type_maker', db.metadata,
    db
.Column('type_id', db.Integer, db.ForeignKey('type.id')),
    db
.Column('maker_id', db.Integer, db.ForeignKey('maker.id')))

# association between vehicle and maker tables
vehicle_maker = db.Table('vehicle_maker', db.metadata,
    db
.Column('maker_id', db.Integer, db.ForeignKey('maker.id')),
    db
.Column('vehicle_id', db.Integer, db.ForeignKey('vehicle.id')))

# association between vehicle and model tables
vehicle_model = db.Table('vehicle_model', db.metadata,
    db
.Column('model_id', db.Integer, db.ForeignKey('model.id')),
    db
.Column('vehicle_id', db.Integer, db.ForeignKey('vehicle.id')))

# association between vehicle and version tables
vehicle_version = db.Table('vehicle_version', db.metadata,
    db
.Column('version_id', db.Integer, db.ForeignKey('version.id')),
    db
.Column('vehicle_id', db.Integer, db.ForeignKey('vehicle.id')))

# association between vehicle and feature tables
vehicle_feature = db.Table('vehicle_feature', db.metadata,
    db
.Column('vehicle_id', db.Integer, db.ForeignKey('vehicle.id')),
    db
.Column('feature_id', db.Integer, db.ForeignKey('feature.parent_id')))



class Type(db.Model):
   
"""VehicleType
    """
    __tablename__ = 'type'
    id = db.Column(db.Integer, primary_key=True)
    name
= db.Column(db.String(50), unique=True, index=True)

    makers
= db.relationship('Maker', secondary=type_maker, backref=db.backref('type'))


   
def __repr__(self):
       
return '<Type #{}: {}>'.format(self.id, self.name)


class Maker(db.Model):
   
"""VehicleMaker
    """
    __tablename__ = 'maker'
    id = db.Column(db.Integer, primary_key=True)
    name
= db.Column(db.String(50), index=True)
    versions
= db.relationship('Version', backref=db.backref('maker'))

   
def __repr__(self):
       
return '<Maker #{}: {}>'.format(self.id, self.name)


class Version(db.Model):
   
"""VehicleVersion
    """
    __tablename__ = 'version'
    id = db.Column(db.Integer, primary_key=True)
    maker_id
= db.Column(db.Integer, db.ForeignKey('maker.id'))
    name
= db.Column(db.String(50), index=True)
    code
= db.Column(db.String(10), index=True)
    models
= db.relationship('Model', backref=db.backref('version'))

   
def __repr__(self):
       
return '<Version #{}: {}>'.format(self.id, self.name)


class Model(db.Model):
   
"""VehicleModel
    """
    __tablename__ = 'model'
    id = db.Column(db.Integer, primary_key=True)
    version_id
= db.Column(db.Integer, db.ForeignKey('version.id'))
    name
= db.Column(db.String(50), index=True)
    code
= db.Column(db.String(10), index=True)
    price
= db.Column(db.Float(precision=2), index=True)

   
def __repr__(self):
       
return '<Model #{}: {}>'.format(self.id, self.name)



Em quarta-feira, 13 de maio de 2015 12:49:43 UTC-3, willian coelho escreveu:

Mike Bayer

unread,
May 13, 2015, 1:48:26 PM5/13/15
to sqlal...@googlegroups.com
please run the attached.  your code runs fine.




On 5/13/15 11:49 AM, willian coelho wrote:
test.py

willian coelho

unread,
May 13, 2015, 1:52:14 PM5/13/15
to sqlal...@googlegroups.com
Pretty good, it worked fine!


Em quarta-feira, 13 de maio de 2015 12:49:43 UTC-3, willian coelho escreveu:
Reply all
Reply to author
Forward
0 new messages