MySQLを利用したFlask RESTful API作成(1)

MySQLを利用したFlask RESTful API作成メモ

https://qiita.com/KWS_0901/items/8384d7b2961a3c682afe

↓これでインストールした

sudo python3 -m pip install flask SQLAlchemy Flask-SQLAlchemy marshmallow marshmallow-sqlalchemy flask-marshmallow pymysql

CentOS7にMySQLをインストールする

MySQL 5.7 を CentOS 7 に yum インストールする手順
https://weblabo.oscasierra.net/installing-mysql57-centos7-yum/
CentOS7にMySQLをインストールして初期設定するまで
https://qiita.com/nooboolean/items/7efc5c35b2e95637d8c1

MySQLのパスワードポリシーを変更する

Mysql 5.7* パスワードをPolicyに合わせるとめんどくさい件について
https://qiita.com/keisukeYamagishi/items/d897e5c52fe9fd8d9273

MySQLでデータベースとテーブルの文字コードがlatin1だったのでUTF-8にする

https://qiita.com/toshihirock/items/fdbc8e7b889e0d17a18d

実行してみる

実行してみる

$ python3 app.py 
Traceback (most recent call last):
  File "app.py", line 4, in 
    from marshmallow_sqlalchemy import ModelSchema
ImportError: cannot import name 'ModelSchema'


app.py の修正
コメントアウトする
#from marshmallow_sqlalchemy import ModelSchema


実行してみる

$ python3 app.py
/usr/local/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:873: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
  File "app.py", line 39, in 
    class UserSchema(ma.ModelSchema):
AttributeError: 'Marshmallow' object has no attribute 'ModelSchema'


'Marshmallow' object has no attribute 'ModelSchema'
https://stackoverflow.com/questions/57984649/marshmallow-object-has-no-attribute-modelschema


app.pyの修正
#class UserSchema(ma.ModelSchema): ←コメントアウトし、以下の行を追加する
class UserSchema(ma.SQLAlchemyAutoSchema):


実行してみる

$ python3 app.py
/usr/local/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:873: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
/usr/local/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:873: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
 * Debugger is active!
 * Debugger PIN: 130-427-693

おっ!


getする

$ curl http://localhost:5000/users/1/
...
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")
(Background on this error at: http://sqlalche.me/e/14/e3q8)

-->


DB接続設定のところをなにも修正してなかった。。
app.pyの修正
↓id, パスワード, ポート番号を変更した
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://******:******@127.0.0.1:3306/sample_db'


実行してみる。

$ curl http://localhost:5000/users/1/
{}
$ curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "email_address":"test@example.com"}' http://localhost:5000/users/
{
  "email_address": "test@example.com", 
  "name": "test"
}
$ curl -X POST -H "Content-Type: application/json" -d '{"name":"test2", "email_address":"test2@example.com"}' http://localhost:5000/users/
{
  "email_address": "test2@example.com", 
  "name": "test2"
}
$ curl http://localhost:5000/users/1/
{
  "email_address": "test@example.com", 
  "id": 1, 
  "name": "test"
}
$ curl http://localhost:5000/users/2/
{
  "email_address": "test2@example.com", 
  "id": 2, 
  "name": "test2"
}


MySQLを利用したFlask RESTful API作成(2) - マイノート