gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:46 24 4
gpt4 key购买 nike

<分区>

全部我有一个关于 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 之间的关系,一切正常。问题是那部分。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com