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

Question:

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:ProjectsOO-IMmodels.py", line 15, in <module>
    Column('followed_id', Integer(), ForeignKey('user.id'))
  File "C:Python27libsite-packagessqlalchemysqlschema.py", line 369, in __new__
    schema = metadata.schema
  File "C:Python27libsite-packagessqlalchemysqlelements.py", line 662, in __getattr__
    key)
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",
                            secondary=friendships,
                            primaryjoin=(friendships.c.follower_id == id),
                            secondaryjoin=(friendships.c.followed_id == id),
                            backref=backref("followers", lazy="dynamic"),
                            lazy="dynamic")

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

        if followed:
            for user in followed:
                self.follow(user)

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

    def unfollow(self, user):
        if self.is_following(user):
            self.followed.remove(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)
Asked By: elonzh

||

Answers:

The table definition should be:

friendships = Table('friendships',
                    Base.metadata,
                    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.

Answered By: Haleemur Ali

I had the same error because I spelled Column with a lowercase c. It should be Column.

Answered By: Hatshepsut

I faced the similar issue. Precisely, the error that I faced was :
AttributeError: Neither 'ColumnClause' object nor 'Comparator' object has an attribute '_set_parent_with_dispatch'

After a lot of struggle, I found out it was a spelling mistake. I spelled sa.Column as sa.column in below snippet line #6.

from alembic import op
import sqlalchemy as sa

def something():
    op.add_column('table_name',
        sa.Column('column_name', sa.Text(), nullable=True) # Correct usage
    )
Answered By: Rahul Kumar