【问题标题】:Flask SQLAlchemy: AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' [duplicate]Flask SQLAlchemy:AttributeError:'Column'对象和'Comparator'对象都没有属性'schema'[重复]
【发布时间】:2016-02-22 13:41:03
【问题描述】:

全部 我对使用 SQL-Alchemy 的 Flask 有疑问

我现在正在使用 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()

而且,这是我在 app 目录之外的 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 与官网给出的示例代码非常相似。

这是错误的 Stacktrace。

/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 类没有 metadat 属性。

标签: python flask


【解决方案1】:

哦,上帝。我找到了解决方案。 即使我可以看到该属性,我只是添加base.metadata。 我只是一些修复数据库语法。这是我做的。

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

【讨论】:

    猜你喜欢
    • 2015-05-29
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2015-12-08
    • 2015-04-14
    • 1970-01-01
    相关资源
    最近更新 更多