How to convert this Python SQLAlchemy code to MySQL?

Panther28

Elite Member
Executive VIP
Jr. VIP
Joined
May 2, 2010
Messages
9,934
Reaction score
16,030
I have the following code:
Python:
class User(db.Model, UserMixin):
    userid = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))

The code is part of a MVC flask website setup, and I'm trying to convert from the preinstalled alchemy to MySQL remote db.

The error is with the models and columns. I'm not even sure this is what I should be doing to convert in the first place.
82d210a792730042f867fed228f5eb04.png

I also get the error message: ImportError: cannot import name 'User' from 'website.models'

Referring to the class User issues obviously.

Hoping someone with a bit of Python knowledge that has converted between the 2 versions of MySQL can help point me in the right direction.

Cheers
 
In pure MySQL language, you would generally create a table like so:

CREATE TABLE name (
col_name_1 type,
col_name_2 type,
);

Note:
You don't need to create things manually if you are using sqlalchemy and have setup models. This should do it:
models.Base.metadata.create_all(bind=engine)
 
Here's an example for sqlalchemy model:

class Dongle(Base):
__tablename__ = "dongle"

id = Column(Integer, primary_key=True, nullable=False, autoincrement="auto")
dongle_number = Column(Integer, unique=True, nullable=False)
 
Back
Top