Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask SQLAlchemy: AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' [duplicate]

Tags:

python

flask

all I have a question on a Flask with SQL-Alchemy

I am now implementing the Poll app using Flask. During the modeling, I'm facing the problem with many to many relationship. Based on the tutorial on Flask official website, I followed it, but I got a problem when I tried to use it.

Here is the models.py code

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()

and, this is my database.py outside of app directory, this is in the root directory of my project.

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)

You can notify that, database.py is very similar to given example code on official website.

Here is the Stacktrace of the error.

/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

How do I solve it? I don' know where I can start to fix it. If I remove the identifier part, and the relationship between User, and Poll, everything works fine. the problem is that part.

like image 701
김준호 Avatar asked Nov 01 '25 01:11

김준호


1 Answers

Oh, god. I find a solution. I just add Base.metadata even if I can see the attribute. And I just some fix to database syntax. Here is I did.

respondents_identifier = Table('respondents_identifier',
                               Base.metadata,
                               Column('user_id', Integer, ForeignKey('user.id')),
                               Column('poll_id', Integer, ForeignKey('poll.id')),
                               )
like image 62
김준호 Avatar answered Nov 03 '25 16:11

김준호



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!