2017-04-16 15 views
0

Google App Engineを使用してFlaskをベースにしたWebアプリケーションをセットアップしようとしています。 Webアプリケーションはクライアントからデータを受け取り、処理してデータベースに保存する必要があります。Flask-SQLAlchemyをGoogle App Engine DBに接続することができません

私はフラスコSQLAlchemyのを使用しようとしましたが、私はGoogleのクラウドSQLでそれを設定することができないよ、私は同じプロジェクトでMySQLのDBを作成するためにthis guideを使用しました:

DB on GAE

その後、私は私のメインのPythonコードでそれを使用しようとしている:(私はPyCharmを使用しています)私は、バックグラウンドで次のエラーが表示され、私は私の地元の開発にそれを実行しようとしているときに

app.config('SQLALCHEMY_DATABASE_URI') = 'mysql+mysqldb://[email protected]/Results?unix_socket=/cloudsql/crafty-circlet-164415:psy01' 
app.config['SECRET_KEY'] = 'NglfxE8FOP9pgV8fxpyj' 
db = SQLAlchemy(app) 

class Result(db.Model): 
    id = db.Column(db.Integer, primary_key=True) 
    name = db.Column(db.Text) 
    profession = db.Column(db.Text) 
    year = db.Column(db.Text) 
    pressure_level = db.Column(db.Integer) 
    reported_suc_count = db.Column(db.Integer) 
    marked_suc_count = db.Column(db.Integer) 
    real_suc_count = db.Column(db.Integer) 
    insertion_time = db.Column(db.DateTime) 

    def __init__(self, name, profession, year, pressure_level, reported_suc_count, marked_suc_count, real_suc_count): 
     self.name = name 
     self.profession = profession 
     self.year = year 
     self.pressure_level = pressure_level 
     self.reported_suc_count = reported_suc_count 
     self.marked_suc_count = marked_suc_count 
     self.real_suc_count = real_suc_count 
     self.insertion_time = datetime.utcnow() 

@app.route('/resultform', methods=['POST', 'GET']) 
def resultform(): 
    if request.method == 'POST': 
     if not request.form['successmatrices']: 
      flash('please fill all the fields', 'error') 
     else: 
      if 'name' in request.form: 
       name = request.form['name'] 
      else: 
       name = None 
      if 'profession' in request.form: 
       profession = request.form['profession'] 
      else: 
       profession = None 
      if 'year' in request.form: 
       year = request.form['year'] 
      else: 
       year = None 
      if 'pressure_level' in request.form: 
       pressure_level = int(request.form['pressure_level']) 
      else: 
       pressure_level = None 
      if 'successmatrices' in request.form: 
       successmatrices = int(request.form['successmatrices']) 
      else: 
       successmatrices = 0 
      new_result = Result(name=name, profession=profession, year=year, pressure_level=pressure_level, reported_suc_count=successmatrices, marked_suc_count=len(session['marked']), real_suc_count=len(session['correct'])) 
      db.session.add(new_result) 
      db.session.commit() 
      return redirect(url_for('showresults')) 
    return render_template("resultform.html") 

@app.route('/showresults') 
def showresults(): 
    return render_template("showresults.html", results=Results.query.all()) 

if __name__ == '__main__': 
    db.create_all() 
    app.run(debug=True) 

ERROR 2017-04-16 09:20:19,802 wsgi.py:263] 
Traceback (most recent call last): 
    File "C:\Users\<>\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 240, in Handle 
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler()) 
    File "C:\Users\<>\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 299, in _LoadHandler 
    handler, path, err = LoadObject(self._handler) 
    File "C:\Users\<>\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 85, in LoadObject 
    obj = __import__(path[0]) 
    File "C:\Users\<>\PycharmProjects\crafty-circlet-164415\main.py", line 7 
    app.config('SQLALCHEMY_DATABASE_URI') = 'mysql+mysqldb://[email protected]/Results?unix_socket=/cloudsql/crafty-circlet-164415:psy01' 
SyntaxError: can't assign to function call 

と展開した後、次のエラーをGAEに見える:

Error: Server Error 
The server encountered an error and could not complete your request. 

Please try again in 30 seconds. 

これを解決する方法任意のアイデア?

+0

とも。 –

答えて

0

app.configは辞書なので、設定値を追加するには()の代わりに[ ]を使用します。これはapp.config['SECRET_KEY']と同じです。

だから、次のようになります。

app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI 

いくつかの他のポインタすぎ接続に成功します。あなたは正しく接続の詳細をフォーマットする必要があります:

USER = 'root' 
PASSWORD = 'your-cloudsql-password' 
DATABASE = 'your-cloudsql-database-name' 
# connection_name is of the format `project:region:your-cloudsql-instance` 
CONNECTION_NAME = 'your-cloudsql-connection-name' 

SQLALCHEMY_DATABASE_URI = (
    'mysql+pymysql://{user}:{password}@localhost/{database}' 
    '?unix_socket=/cloudsql/{connection_name}').format(
     user=USER, password=PASSWORD, 
     database=DATABASE, connection_name=CONNECTION_NAME) 

app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI 

私はおそらく

など、ソースにチェックされていない設定ファイルの中に私の秘密と他のすべての機密情報を分離したり、環境変数を使用します

ローカルでCloud SQLインスタンスを使用してアプリケーションをテストしたい場合は、あなたがthe Cloud SQL Proxyをインストールしてapp.yaml

> cloud_sql_proxy -instances=your-connection-name=tcp:3306 
に環境変数として接続名と MySQLdbライブラリを追加する必要があります

これ以外の場合は、ローカルのMySQLインスタンスを使用してテストすることはできますが、アプリケーションエンジンの場合はCloud SQLに切り替えることができます。あなたはベータ版の設定app.yamlをファイル(フレックスENV)の下で、クラウドSQLインスタンス名を含める必要が

More information on setting up Cloud SQL with App Engine can be found here

関連する問題