earlzo earlzo - 3 years ago 277
Python Question

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema'

I tried creating a new database in my project, but I got this error when I run the script, I have another project using similar definition, it worked before, but now it get the same error now.
I am using Python 2.7.8 and the version of SQLAlchemy module is 0.9.8.
By the way, a project used Flask-SQLAlchemy, it works well.
I am confused.
The traceback information is following:

Traceback (most recent call last):
File "D:/Projects/OO-IM/db_create.py", line 4, in <module>
from models import Base
File "D:\Projects\OO-IM\models.py", line 15, in <module>
Column('followed_id', Integer(), ForeignKey('user.id'))
File "C:\Python27\lib\site-packages\sqlalchemy\sql\schema.py", line 369, in __new__
schema = metadata.schema
File "C:\Python27\lib\site-packages\sqlalchemy\sql\elements.py", line 662, in __getattr__
AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema'

from sqlalchemy import create_engine, Column, String, Integer, Text, DateTime, Boolean, ForeignKey, Table
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base

SQLALCHEMY_DATABASE_URI = "mysql://root:mysqladmin@localhost:3306/oo_im?charset=utf8"

Base = declarative_base()

# TODO:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema'
friendships = Table('friendships',
Column('follower_id', Integer(), ForeignKey('user.id')),
Column('followed_id', Integer(), ForeignKey('user.id'))

class User(Base):
__tablename__ = 'user'
id = Column(Integer(), primary_key=True)
account = Column(String(32), unique=True, nullable=False)
password = Column(String(32), nullable=False)
followed = relationship("User",
primaryjoin=(friendships.c.follower_id == id),
secondaryjoin=(friendships.c.followed_id == id),
backref=backref("followers", lazy="dynamic"),

def __init__(self, account, password, followed=None):
self.account = account
self.password = password

if followed:
for user in followed:

def follow(self, user):
if not self.is_following(user):
return self

def unfollow(self, user):
if self.is_following(user):
return self

def is_following(self, user):
return self.followed.filter(friendships.c.followed_id == user.id).count() > 0

class ChatLog(Base):
__tablename__ = 'chatlog'
id = Column(Integer(), primary_key=True)
sender_id = Column(Integer(), ForeignKey('user.id'), nullable=False)
receiver_id = Column(Integer(), ForeignKey('user.id'), nullable=False)
send_time = Column(DateTime(), nullable=False)
received = Column(Boolean(), default=False)
content = Column(Text(), nullable=False)

engine = create_engine(SQLALCHEMY_DATABASE_URI, convert_unicode=True)
DBSession = sessionmaker(bind=engine)

Answer Source

The table definition should be:

friendships = Table('friendships',
                    Column('follower_id', Integer(), ForeignKey('user.id')),
                    Column('followed_id', Integer(), ForeignKey('user.id'))

When defining tables using the declarative syntax, the metadata is inherited through the class declaration from Base, i.e.

Base = declarative_base()

class ChatLog(Base)

but, when defining tables using the old Table syntax, the metadata must be explicitly specified.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download