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