Python SQLAlchemy:AttributeError:'Column'对象和'Comparator'对象都没有属性'schema'

ear*_*lzo 15 python sqlalchemy attributeerror

我尝试在我的项目中创建一个新数据库,但是当我运行脚本时遇到了这个错误,我有另一个使用类似定义的项目,它之前有效,但现在它得到了同样的错误.我使用的是Python 2.7.8,SQLAlchemy模块的版本是0.9.8.顺便说一句,一个项目使用Flask-SQLAlchemy,它运行良好.我很迷惑.回溯信息如下:

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)
Run Code Online (Sandbox Code Playgroud)

Hal*_*Ali 23

表定义应该是:

friendships = Table('friendships',
                    Base.metadata,
                    Column('follower_id', Integer(), ForeignKey('user.id')),
                    Column('followed_id', Integer(), ForeignKey('user.id'))
)
Run Code Online (Sandbox Code Playgroud)

使用声明性语法定义表时,元数据通过Base的类声明继承,即

Base = declarative_base()

class ChatLog(Base)
Run Code Online (Sandbox Code Playgroud)

但是,在使用旧的Table语法定义表时,必须明确指定元数据.


Hat*_*sut 8

我有同样的错误,因为我Column用小写拼写c.它应该是Column.

  • 我和你有同样的错误,我需要花很多时间来解决它.非常感谢. (2认同)