인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

YH K님의 프로필 이미지
YH K

작성한 질문수

실전! FastAPI 입문

session.scalars(select(ToDo))에러

해결된 질문

작성

·

182

0

orm.py를 다음과 같이 작성했고

from sqlalchemy import Boolean, Column, Integer, String
from sqlalchemy.orm import declarative_base

Base = declarative_base() # base란 클래스로 db모델링

class ToDo(Base):
    __tablename__ = 'todo' # table이름

    id = Column(Integer, primary_key=True, index=True)
    content = Column(String(256), nullable=False)
    is_done = Column(Boolean, nullable=False)

    # todo 객체의 출력을 쉽게 보기위해 repr
    def __repr__(self):
        return f'<ToDo(id={self.id}, content={self.content})>, is_done={self.is_done}'

connection.py도 다음과 같이 작성했으나

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

DATABASE_URL = "mysql+pymysql://root:todos@127.0.0.1:3306/todos"

engine = create_engine(DATABASE_URL, echo=True) # echo는 쿼리의 처리를 확인
SessionFactory = sessionmaker(autocommit=False, autoflush=False, bind=engine)

강의안에서 말씀하신대로 import까지 완료하고

명령어 session.scalars(select(ToDo)) 를 실행하는 과정에서 자꾸 이렇게 뜹니다

 

에러가 한두개가 아니라서 감도 안잡힙니다.. 도와주세요

2024-06-06 20:53:23,126 INFO sqlalchemy.engine.Engine SELECT todo.id, todo.content, todo.is_done

FROM todo

2024-06-06 20:53:23,126 INFO sqlalchemy.engine.Engine [cached since 368.2s ago] {}

Traceback (most recent call last):

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\engine\base.py", line 1967, in _exec_single_context

self.dialect.do_execute(

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\engine\default.py", line 924, in do_execute

cursor.execute(statement, parameters)

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\cursors.py", line 153, in execute

result = self._query(query)

^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\cursors.py", line 322, in _query

conn.query(q)

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\connections.py", line 563, in query

self._affected_rows = self._read_query_result(unbuffered=unbuffered)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\connections.py", line 825, in _read_query_result

result.read()

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\connections.py", line 1199, in read

first_packet = self.connection._read_packet()

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\connections.py", line 775, in _read_packet

packet.raise_for_error()

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\protocol.py", line 219, in raise_for_error

err.raise_mysql_exception(self._data)

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\err.py", line 150, in raise_mysql_exception

raise errorclass(errno, errval)

pymysql.err.OperationalError: (1054, "Unknown column 'todo.content' in 'field list'")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File "C:\Program Files\JetBrains\PyCharm 2024.1.2\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode

coro = func()

^^^^^^

File "<input>", line 1, in <module>

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\orm\session.py", line 2459, in scalars

return self._execute_internal(

^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\orm\session.py", line 2236, in _execute_internal

result: Result[Any] = compile_state_cls.orm_execute_statement(

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\orm\context.py", line 293, in orm_execute_statement

result = conn.execute(

^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\engine\base.py", line 1418, in execute

return meth(

^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\sql\elements.py", line 515, in _execute_on_connection

return connection._execute_clauseelement(

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\engine\base.py", line 1640, in _execute_clauseelement

ret = self._execute_context(

^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\engine\base.py", line 1846, in _execute_context

return self._exec_single_context(

^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\engine\base.py", line 1986, in _exec_single_context

self._handle_dbapi_exception(

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\engine\base.py", line 2353, in _handle_dbapi_exception

raise sqlalchemy_exception.with_traceback(exc_info[2]) from e

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\engine\base.py", line 1967, in _exec_single_context

self.dialect.do_execute(

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\sqlalchemy\engine\default.py", line 924, in do_execute

cursor.execute(statement, parameters)

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\cursors.py", line 153, in execute

result = self._query(query)

^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\cursors.py", line 322, in _query

conn.query(q)

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\connections.py", line 563, in query

self._affected_rows = self._read_query_result(unbuffered=unbuffered)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\connections.py", line 825, in _read_query_result

result.read()

File "C:\Users\yhkim\inflern_project\todos\Lib\site-packages\pymysql\connections.py", line 1199, in read

first_packet = self.connection._read_packet()

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 

 

답변 1

0

신동현님의 프로필 이미지
신동현
지식공유자

안녕하세요. 커넥션 쪽에 문제인 것 같은데 데이터베이스가 잘 동작 중인게 맞을까요? session.scalars(select(1)) 입력시 어떻게 출력되나요?

YH K님의 프로필 이미지
YH K
질문자

아! contents를 content로 입력해서 오류가 났었네요. 해결했습니다. 감사합니다

YH K님의 프로필 이미지
YH K

작성한 질문수

질문하기