通过sqlalchemy添加一个对象抛出KeyError:'SQLALCHEMY_TRACK_MODIFICATIONS' [英] Adding an object via sqlalchemy throws KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'
问题描述
这是db.session.add(newTag)引发异常的视图。
$ b $ pre $ def $(self) :
json_data = request.get_json(force = True)
foo = json_data ['foo'] if'foo'in json_data else''
bar = json_data ['bar '] if'bar'in json_data else''
baz = json_data ['baz'] if'baz'in json_data else''
try:
boo = parse(json_data ['boo '])
far = parse(json_data ['far'])
除外:
返回jsonify(result ='无效日期')
faz = json_data [ 'faz']如果'faz'in json_data else''
if faz =='':
返回jsonify(result =faz is missing)
newTag = Tags(faz,baz,foo,bar,boo,far,True)
db.session.add(newTag)
db.session.commit()
return {'Success ,标签'+ str(newTag.id)+'created'}
我的模型看起来像这样
class标记(db.Model):
id = db.Column(db.Integer,primary_key = True)
faz = db.Column(db.String(255))
foo = db.Column(db.Integer)
bar = db.Column(db.Integer)
baz = db。列(db.Integer)
boo = db.Column(db.DateTime)
far = db.Column(db.DateTime)
status = db.Column(db.Boolean)
def __init __(self,faz,foo,bar,baz,boo,far,status):
self.faz = faz
self.foo = foo
self。 bar = bar
self.baz = baz
self.boo = boo
self.far = far
self.status = status
我不确定是否需要更多的细节来帮助我。
我已经把SQLALCHEMY_TRACK_MODIFICATIONS = False放到了包含我的SQLALCHEMY_DATABASE_URI的config.py文件中。
同样的错误。
这个错误实际上是不明确的。
我在mysql日志中寻找响应,但一切正常。
然后我使用一个老版本的sqlalchemy。
我使用的版本2.1,然后我通过版本1.0。
我得到的错误更加清晰。
要将SQLALCHEMY_TRACK_MODIFICATIONS设置为False或True,绝对不会改变您的错误。
听到的问题是连接输入你的应用程序,你的数据库和你的sqlalchemy。
听到是我的archi
app
main.py
models.py
views
__init__.py
manage.py
views.py
在我的init.py中,我把所有的配置放在
from flask_sqlalchemy从flask进口SQLAlchemy
Flask
$ b app = Flask(__ name__)
app .config ['SQLALCHEMY_DATABASE_URI'] ='mysql + pymysql:// root:password@172.17.0.2/mydatabaseapp'
app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy ()
db.init_app(app)
然后我在我的管理器中创建一个类.py来操纵我的数据库
所以我必须在我的manager.py中导入数据库,我这样做。
我的views.py只是调用我的经理和我的main.py的方法调用我的views.py的功能
但是问题是我总是有这个错误。所以我改变了我的架构。
我在我的应用程序文件夹中创建了一个文件config.py,我只是把来自flask_sqlalchemy的
导入SQLAlchemy
db = SQLAlchemy()
在我的main.py中,我把
$ b $ pre $ app $ c $ app.config ['SQLALCHEMY_DATABASE_URI'] ='mysql + pymysql:// root: password@172.17.0.2/mydatabaseapp'
app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
然后,如果我需要在我的应用程序的某个地方调用分贝,我只是从main.py调用它,并解决了问题。
我不明白的烧瓶,是架构解释你的问题真的很抱歉。我希望这个回应会帮助你。
我认为这个错误通常是在瓶子的配置输入ORM并且你的应用程序不正常的时候出现的。
This is my first python app, I have been struggling to get migrations working correctly with flask and sqlalchemy. I finally got that working but now when I run the application, I get an error when I add an object to the table that was modified with the migration code. This is the view the exception is thrown on db.session.add(newTag)
def put(self):
json_data = request.get_json(force=True)
foo = json_data['foo'] if 'foo' in json_data else ''
bar = json_data['bar'] if 'bar' in json_data else ''
baz = json_data['baz'] if 'baz' in json_data else ''
try:
boo = parse(json_data['boo'])
far = parse(json_data['far'])
except:
return jsonify(result='Invalid date')
faz = json_data['faz'] if 'faz' in json_data else ''
if faz == '':
return jsonify(result="faz is missing")
newTag = Tags(faz, baz, foo, bar, boo, far, True)
db.session.add(newTag)
db.session.commit()
return {'Success, tag' + str(newTag.id) + ' created'}
My model looks like this
class Tags(db.Model):
id = db.Column(db.Integer, primary_key=True)
faz = db.Column(db.String(255))
foo = db.Column(db.Integer)
bar = db.Column(db.Integer)
baz = db.Column(db.Integer)
boo = db.Column(db.DateTime)
far = db.Column(db.DateTime)
status = db.Column(db.Boolean)
def __init__(self, faz, foo, bar, baz, boo, far, status):
self.faz = faz
self.foo = foo
self.bar = bar
self.baz = baz
self.boo = boo
self.far = far
self.status = status
I am not sure if any further detail is needed to assist me. I have put SQLALCHEMY_TRACK_MODIFICATIONS = False in the config.py file that contains my SQLALCHEMY_DATABASE_URI.
I have exactly the same error.
This error is realy not explicit. i look for a response in mysql log but everything is ok.
Then i use an older version of sqlalchemy. I work with the version 2.1 and then i pass in version 1.0. The error i got is more clear.
To put SQLALCHEMY_TRACK_MODIFICATIONS at False or True change absolutely nothing to your error.
The problem hear is the connection enter your app, your db and your sqlalchemy.
hear is my archi
app
main.py
models.py
views
__init__.py
manage.py
views.py
In my init.py i put all my config
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:password@172.17.0.2/mydatabaseapp'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy()
db.init_app(app)
and then i create a class in my manager.py to manipulate my database So i have to import db in my manager.py, i do it.
My views.py just call the methods of my manager and my main.py call the function of my views.py
But the problem is i always have this error. So i change my architecture. I create a file config.py in my app folder i just put
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
in the file. And in my main.py i put
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:password@172.17.0.2/mydatabaseapp'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
then if i need to call db somewhere in my app i just call it from main.py and the problem solves.
I don't understand enough flask and is architecture to explane you the problem really sorry. I hope that reponse will help you. I think this error comes typically when the flask's config enter ORM and your app is not ok.
这篇关于通过sqlalchemy添加一个对象抛出KeyError:'SQLALCHEMY_TRACK_MODIFICATIONS'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!