我尝试在我的项目中创建一个新数据库,但是当我运行脚本时出现此错误,我有另一个使用类似定义的项目,它以前工作过,但现在它现在出现相同的错误。 我使用的是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)
表定义应该是:
friendships = Table('friendships',
Base.metadata,
Column('follower_id', Integer(), ForeignKey('user.id')),
Column('followed_id', Integer(), ForeignKey('user.id'))
)
使用声明式语法定义表时,元数据是通过类声明从Base继承的,即
Base = declarative_base()
class ChatLog(Base)
但是,当使用旧的 Table 语法定义表时,必须显式指定元数据。
我有同样的错误,因为我将
Column
拼写为小写 c
。应该是Column
。
我也遇到过类似的问题。准确地说,我遇到的错误是:
AttributeError: Neither 'ColumnClause' object nor 'Comparator' object has an attribute '_set_parent_with_dispatch'
经过一番挣扎,我发现这是一个拼写错误。我在下面的代码片段行 #6 中将 sa.Column 拼写为 sa.column。
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
)
我以一种非常愚蠢的方式收到了同样的错误消息。我写道:
Table(name=name, metadata=metadata, *columns)
当我应该写:
Table(name, metadata, *columns)