Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Study] 採用 ORM framework #42

Closed
wants to merge 6 commits into from

Conversation

s9891326
Copy link
Contributor

Why need this change? / Root cause:

Changes made:

  • models.py:對應到create_tables.sql的3張表(partial index沒建,其他都先參照原本的屬性)
  • tutorial.md:存放相關技術文件
  • /migrations:flask-migrate後的產物

Test Scope / Change impact:

  1. 改用測試的DB
# repository/app.py
app.config['SQLALCHEMY_DATABASE_URI'] = \
    f"postgresql://{pt_config.DB_USER}:{pt_config.DB_PASSWORD}@{pt_config.DB_HOST}:5432/test"
  1. 在postgres中新增test database
>> psql -U postgres
>> create database test;
  1. 創建tables
flask --app repository/models.py db upgrade

@s9891326 s9891326 changed the title Initial flask-migrate, and create models.py(GoodInfo、User、UserSubGood) [Study] 採用 ORM framework Dec 22, 2022
@s9891326
Copy link
Contributor Author

Q: SQL UPDATE 那UPDATE TIME 會更新嗎?
A: 不會 GG。目前找到的語法如下

@auto_commit
def update_good_stock_state(good_id: str, stock_state: int, **kwargs):
    # 對應到 update_good_stock_state(good_id, state)
    # r = GoodInfo.query.filter_by(id=good_id).update({"stock_state": stock_state})
    r = db.engine.execute(text("update good_info set stock_state=:stock_state where id=:good_id"),
                          {"stock_state": stock_state, "good_id": good_id})
    return r

@chen-tf
Copy link
Owner

chen-tf commented Dec 24, 2022

Q: SQL UPDATE 那UPDATE TIME 會更新嗎? A: 不會 GG。目前找到的語法如下

@auto_commit
def update_good_stock_state(good_id: str, stock_state: int, **kwargs):
    # 對應到 update_good_stock_state(good_id, state)
    # r = GoodInfo.query.filter_by(id=good_id).update({"stock_state": stock_state})
    r = db.engine.execute(text("update good_info set stock_state=:stock_state where id=:good_id"),
                          {"stock_state": stock_state, "good_id": good_id})
    return r

也算是合乎預期,可以上 workaround,update 的時候要加上 update_time 欄位的變更,且使用 postgresql 的 now()

@s9891326 s9891326 force-pushed the feature/study_sqlalchemy_flask_migrate branch from d7f05bf to bc89e3c Compare December 25, 2022 09:11
@s9891326 s9891326 marked this pull request as ready for review December 25, 2022 09:12
Comment on lines +120 to +122
@app_context
def add_good_info(good_info):
return pt_repository.add_good_info(good_info)
create_good_info(good_info.good_id, good_info.price, good_info.name, good_info.stock_state)
Copy link
Contributor Author

@s9891326 s9891326 Dec 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增加@app_context來簡化之前說到要用下面這個才能正常跑的問題

with app.app_context():
    xxxx

@s9891326 s9891326 closed this Jan 3, 2023
@chen-tf chen-tf deleted the feature/study_sqlalchemy_flask_migrate branch April 4, 2023 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants