问题
Main.py
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = '123'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
__table_name__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
password = db.Column(db.String(80))
def __init__(self, username, password):
self.username = username
self.password = password
@app.route('/register/', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
"""Check username is available"""
data = User.query.filter_by(username=username).first()
if data is None:
new_user = User(username=username, password=password)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register2.html')
return render_template('register1.html')
I deployed my web app through Google App Engine but when I try to register, I am currently have operational error on Google App Engine.
OperationalError: (sqlite3.OperationalError) attempt to write a readonly database
It seems like files are read-only in Google App Engine storage.
Is there any way to write .db file in GAE?
回答1:
Writing to local files is not supported in App Engine, this is mainly because of the way that App Engine scales your app, by creating multiple parallel instances. If this was not the case and you keep writing to files in each local file system, inconsistencies will be generated, which is not what you want.
Ideally for App Engine you should be using the setup that follow:
For DB Storage: Use a Cloud Based Database, in your case, I believe that you could use CloudSQL, but you can find the product that Google Cloud offers for databases here to check what better suits you app.
For File Storage: Use a Cloud File Storage System. Google Cloud offers the Cloud Storage product for that purpose, you can check more details here.
来源:https://stackoverflow.com/questions/62011326/flask-sqlalchemy-google-app-engine-no-permission-to-write