python - flask SQLAlchemy : AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema'

标签 python flask

<分区>

全部 我有一个关于 Flask with SQL-Alchemy 的问题

我现在正在使用 Flask 实现投票应用程序。 在建模期间,我面临着多对多关系的问题。按照Flask官网的教程,照做了,但是在尝试使用的时候遇到了问题。

这是models.py代码

from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Table, PrimaryKeyConstraint
from sqlalchemy.orm import relationship
from database import Base
from datetime import datetime


respondents_identifier = Table('respondents_identifier',

                               Column('user_id', Integer, ForeignKey('users.id')),
                               Column('poll_id', Integer, ForeignKey('polls.id')),
                               )


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String(20))
    name_string = Column(String(100), unique=True)

    def __init__(self, name=None, name_string=None):
        self.name = name
        self.name_string = name_string

    def __repr__(self):
        return '<User %r %r>' % self.name, self.nameString


class Poll(Base):
    __tablename__ = 'poll'
    id = Column(Integer, primary_key=True)
    subject = Column(String(50))
    question_statement = Column(String(100))
    num_questions = Column(Integer)  # 응답지 개수
    total_participant = Column(Integer)  # 총 참여자 수
    questions = relationship('Question', backref='Poll')
    comments = relationship('Comment', backref='Poll')
    respondents = relationship("User",
                               secondary=respondents_identifier)

    def __init__(self, subject=None, question_statement=None, num_questions=2):
        self.subject = subject
        self.question_statement = question_statement
        self.num_questions = num_questions
        self.total_participant = 0


class Question(Base):
    __tablename__ = 'question'
    id = Column(Integer, primary_key=True)
    choice_num = Column(Integer)
    answer_description = Column(String(50))
    poll_id = Column(Integer, ForeignKey('poll.id'))
    selected_num = Column(Integer)  # 선택된 수

    def __init__(self, choice_num, answer_description=None):
        self.choice_num = choice_num
        self.answer_description = answer_description
        self.selected_num = 0

    def __repr__(self):
        return '<Poll %d %r>' % self.answer_num, self.answer_description

    def set_poll_id(self, poll):
        self.poll_id = poll.id


class Comment(Base):
    __tablename__ = 'comment'
    id = Column(Integer, primary_key=True)
    comment_content = (String(200))
    user_name = (String(20))
    poll_id = Column(Integer, ForeignKey('poll.id'))
    comment_time = Column(DateTime)

    def __init__(self, user_name, comment_content):
        self.user_name = user_name
        self.comment_content = comment_content
        self.comment_time = datetime.now()

而且,这是我在应用程序目录外的 database.py,它在我的项目的根目录中。

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()


def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    import HotOpinion.models
    Base.metadata.create_all(bind=engine)

您可以注意到,database.py 与官网给出的示例代码非常相似。

这是错误的堆栈跟踪。

/Users/junojunho/.pyenv/versions/hotopinion/bin/python /Users/junojunho/Documents/github/HotOpinion/runserver.py
Traceback (most recent call last):
  File "/Users/junojunho/Documents/github/HotOpinion/runserver.py", line 15, in <module>
    init_db()
  File "/Users/junojunho/Documents/github/HotOpinion/database.py", line 17, in init_db
    import HotOpinion.models
  File "/Users/junojunho/Documents/github/HotOpinion/HotOpinion/models.py", line 11, in <module>
    Column('poll_id', Integer, ForeignKey('polls.id')),
  File "/Users/junojunho/.pyenv/versions/hotopinion/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 374, in __new__
    schema = metadata.schema
  File "/Users/junojunho/.pyenv/versions/hotopinion/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 735, in __getattr__
    key)
AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema'

Process finished with exit code 1

我该如何解决?我不知道从哪里开始修复它。 如果我删除标识符部分,以及 User 和 Poll 之间的关系,一切正常。问题是那部分。

最佳答案

天哪。我找到了解决办法。 我只是添加 Base.metadata 即使我可以看到该属性。 我只是对数据库语法进行了一些修复。这是我做的。

respondents_identifier = Table('respondents_identifier',
                               Base.metadata,
                               Column('user_id', Integer, ForeignKey('user.id')),
                               Column('poll_id', Integer, ForeignKey('poll.id')),
                               )

关于python - flask SQLAlchemy : AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33820403/

相关文章:

python - 如何在 python 中更改 ADO 结果集格式?

python - Python 嵌套 For 循环错误

在 Python/Flask 中测试计划任务

python - 处理 Flask 请求中的嵌套数组

python - python和 Elasticsearch 更新错误

Python - 绘制多边形

python - 使用分隔符将文件中的多行存储到变量

python - 基于多个数据点的评分

python - 使用 _mysql 修复 SQL 注入(inject)

python - 将 Flask 环境默认设置为开发模式?