1

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.

0

1 Answer 1

5

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:[email protected]/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:[email protected]/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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.