Python SQLAlchemy:AttributeError:“列"对象和“比较器"对象都没有属性“模式" [英] Python SQLAlchemy: AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema'

查看:248
本文介绍了Python SQLAlchemy:AttributeError:“列"对象和“比较器"对象都没有属性“模式"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在项目中创建一个新的数据库,但是在运行脚本时遇到此错误,我有另一个使用类似定义的项目,该项目以前可以使用,但现在却出现相同的错误. 我正在使用Python 2.7.8,SQLAlchemy模块的版本是0.9.8. 顺便说一句,一个项目使用Flask-SQLAlchemy,它运行良好. 我很迷惑. 追溯信息如下:

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:\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的类声明继承,即

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)

但是,当使用旧的Table语法定义表时,必须显式指定元数据.

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

这篇关于Python SQLAlchemy:AttributeError:“列"对象和“比较器"对象都没有属性“模式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆