所有 我在带有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
我该如何解决?我不喜欢&#39;知道我可以在哪里开始修复它。 如果我删除标识符部分,以及User和Poll之间的关系,一切正常。问题在于那部分。
答案 0 :(得分:3)
哦,上帝。我找到了解决方案。 我只是添加Base.metadata即使我可以看到属性。 我只修复了数据库语法。我做到了。
respondents_identifier = Table('respondents_identifier',
Base.metadata,
Column('user_id', Integer, ForeignKey('user.id')),
Column('poll_id', Integer, ForeignKey('poll.id')),
)