본문 바로가기

MLOps 개발자 양성과정/flask

[Day-78] 플라스크③ (모델 수정하기)

SQLite 데이터베이스는 ORM을 사용할 때 몇 가지 문제점이 있다.

이것은 SQLite 데이터베이스에만 해당하고 PostgreSQL이나 MySQL 등의 다른 데이터베이스와는 상관없는 내용이다.

 

(myproject) C:\projects\myproject>flask db migrate
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added column 'question.user_id'
INFO  [alembic.autogenerate.compare] Detected added foreign key (user_id)(id) on table question
Generating C:\projects\myproject\migrations\versions\0283a2f7fd44_.py ...  done

0283a2f7fd44_.py


(myproject) C:\projects\myproject>flask db upgrade 
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: _alembic_tmp_question.user_id [SQL: INSERT INTO _alembic_tmp_question (id, subject, content, create_date) SELECT question.id, question.subject, question.content, question.create_date FROM question] (Background on this error at: http://sqlalche.me/e/gkpj)

(myproject) c:\projects\myproject>
flask db migrate
INFO [alembic.runtime.migration] Context impl SQLiteImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL.
ERROR [root] Error: Target database is not up to date.

리비전 되돌려야 해

# 현재 migrate 작업의 최종 리비전을 확인
(myproject) C:\projects\myproject>flask db heads  
C:\projects\myproject\pybo\models.py:11: SAWarning: Can't validate argument 's
erver_defalut'; can't locate any SQLAlchemy dialect named 'server'
  user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE')
, nullable=True, server_defalut='1')
0283a2f7fd44 (head)

# 현재 시점의 리비전 확인
(myproject) C:\projects\myproject>flask db current
C:\projects\myproject\pybo\models.py:11: SAWarning: Can't validate argument 's
erver_defalut'; can't locate any SQLAlchemy dialect named 'server'
  user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE')
, nullable=True, server_defalut='1')
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
8f0d7f5d5efb

# 현재 리비전 위치 heads 위치로 돌리기
(myproject) C:\projects\myproject>flask db stamp heads
C:\projects\myproject\pybo\models.py:11: SAWarning: Can't validate argument 's
erver_defalut'; can't locate any SQLAlchemy dialect named 'server'
  user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE')
, nullable=True, server_defalut='1')
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running stamp_revision 8f0d7f5d5efb -> 0283a
2f7fd44

 

flask history