Home > database >  Flask heroku app won't let me assign some config settings
Flask heroku app won't let me assign some config settings

Time:02-13

I have a flask MySql app that is working correctly on local, but when I deployed to heroku it gives me below error:

2022-02-12T05:28:26.114583 00:00 app[web.1]: File "/app/app.py", line 38, in <module>
2022-02-12T05:28:26.114583 00:00 app[web.1]: app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
2022-02-12T05:28:26.114584 00:00 app[web.1]: TypeError: 'str' object does not support item assignment

It did the same thing when I used app.debug = False. I tried to fix it my removing it, but now it gives me above error.

I tried commenting each line that gives error and looks like all below lines give the same error:

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SESSION_PERMANENT'] = False
app.config['SESSION_TYPE'] = "filesystem"

Few lines before these lines I have app.config['MAIL_USE_SSL'] = True and some other mail config settings (port, server, username, password) which none of them give error, so I don't understand what the problem is. When I google the error it is related with the immutability of strings, so I don't understand what is the problem on my case.

When I comment all of the above it gives me:

2022-02-12T08:11:34.109191 00:00 app[web.1]: Session(app)
2022-02-12T08:11:34.109196 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/flask_session/__init__.py", line 54, in __init__
2022-02-12T08:11:34.109196 00:00 app[web.1]: self.init_app(app)
2022-02-12T08:11:34.109196 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/flask_session/__init__.py", line 61, in init_app
2022-02-12T08:11:34.109197 00:00 app[web.1]: app.session_interface = self._get_interface(app)
2022-02-12T08:11:34.109197 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/flask_session/__init__.py", line 64, in _get_interface
2022-02-12T08:11:34.109197 00:00 app[web.1]: config = app.config.copy()
2022-02-12T08:11:34.109198 00:00 app[web.1]: AttributeError: 'str' object has no attribute 'copy'

I'm using JawsDB MySQL addon on heroku if that is related with this issue.

Below is the code:

from flask import Flask
from flask_mail import Mail
from os import environ
from flask_session import Session
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['MAIL_SERVER'] = 'smtp.domain.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = "[email protected]"
app.config['MAIL_PASSWORD'] = environ.get("MAIL_PASSWORD")
mail = Mail(app)

MY_APP_ENV = environ.get("MY_APP_ENV")
if MY_APP_ENV == 'prod':
    app.debug = False
    app.config = environ.get("JAWSDB_URL")
else:
    app.debug = True
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/my_app'

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SESSION_PERMANENT'] = False
app.config['SESSION_TYPE'] = "filesystem"
Session(app)
CORS(app)

db = SQLAlchemy(app)
# rest is the routings etc.

CodePudding user response:

Here's the problem:

app.config = environ.get("JAWSDB_URL")

You are overwriting app.config with a single value from the environment.

Do you mean something like this instead?

app.config["SQLALCHEMY_DATABASE_URI"] = environ.get("JAWSDB_URL")
  • Related