1

I recently ran into a DB Lock issue while building my Flask App using a SQLite Database. Apparently there's no real solution to this and from everything I read it seemed the best workaround would be just to switch over to a 'real' database like MySQL OR PostgreSQL. While doing this however I ran into the following problem:

AttributeError: 'MySQL' object has no attribute 'Model'

I guess my issue here is - Im not sure if I configured it properly in my app.py file AND/OR i'm not sure if i'm using the correct 'syntax(?)' in my models.py file. Or possibly it's neither of these things... Either way, I'd immensely appreciate it if someone could look at my code and tell me what's going on ...

Here is my app.py [what's relevent]

from flask import Flask
from flask_mysqldb import MySQL
from flask_bootstrap import Bootstrap
from flask_mail import Mail, Message 
from flask_moment import Moment 
from flask_login import LoginManager, login_required 
from flask_pagedown import PageDown 
from flask_migrate import Migrate, MigrateCommand
from flask_sslify import SSLify
from werkzeug.wsgi import DispatcherMiddleware
from flask_script import Manager 

server=flask.Flask(__name__)

basedir = os.path.abspath(os.path.dirname(__file__))

server.config['SQLALCHEMY_DATABASE_URI'] = 
'mysql+pymysql://username:password@localhost/db_name'

server.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
server.config.update(dict(
SECRET_KEY="....",
WTF_CSRF_SECRET_KEY="...."
#SECRET_KEY="powerful secretkey",
#WTF_CSRF_SECRET_KEY="a csrf secret key" 
))

manager = Manager(server)
db = MySQL(server) 
#db = SQLAlchemy(server) [Old Code]
migrate = Migrate(server, db)
manager.add_command('db', MigrateCommand) 

Here is my models.py:

from datetime import datetime 
from werkzeug.security import generate_password_hash, 
check_password_hash
from flask import request, current_app, url_for 
from hashlib import md5
from app import db
from app import login_manager
from flask import request, current_app
from flask_login import UserMixin, AnonymousUserMixin  
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
import hashlib 
from markdown import markdown
import bleach
from datetime import datetime 

class Role(db.Model): 
    __tablename__ = 'roles'
    __table_args__ = {'extend_existing': True} 
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(64), unique=True)
    default = db.Column(db.Boolean, default=False, index=True)
    permissions = db.Column(db.Integer) 
    users = db.relationship('User', backref='role', lazy='dynamic')

Any help here would be greatly appreciated !

0

1 Answer 1

2

If you want to use MySQL by SQLAlchemy (Flask-SQLAlchemy), keep the old code for db instance:

# db = MySQL(server)  # remove this line and the import for flask_mysqldb 
db = SQLAlchemy(server)  # keep this and the import for flask_sqlalchemy

Otherwise, if you want to use by Flask-MySQLdb, then you should delete everything comes form SQLAlchemy (Flask-SQLAlchemy), including configuration variables (SQLALCHEMY_DATABASE_URI, SQLALCHEMY_COMMIT_ON_TEARDOWN), imports, db.Model etc.

After that, you should go to read Flask-MySQLdb's documentation to learn how to use it.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.