Python SQLAlchemy: AttributeError: Neither 'column' object nor 'comparator' object has attribute 'schema'


Elon Day

I tried to create a new database in my project, but I get this error when running the script, I have another project with a similar definition that used to work but now I get the same error. I am using Python 2.7.8 and the version of the SQLAlchemy module is 0.9.8. By the way, one project uses Flask-SQLAlchemy and it works fine. I'm confused. The traceback information is as follows:

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__
    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)
Haleemur Ali

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, metadata is inherited through Base's class declaration, i.e.

Base = declarative_base()

class ChatLog(Base)

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

Related


SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"

WiGeeky: I have two tables named usersand permissionsI want to create a relationship between them using the specified table userPermissions. Here's what my code looks like: 类User(Base): __tablename__ ='用户' id =列(Integer,primary_key = True) first_

AttributeError 'SQLAlchemy' object has no attribute 'create'

Sachin Mena I am trying to create a database using sqlalchemy and I am getting db.create.all()this error: File "<stdin>", line 1, in <module> AttributeError: 'SQLAlchemy' object has no attribute 'create' my code: from flask import Flask, render_template from