Models

Creating a model

Here is a sample model which uses sqlalchemy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20

class ContactMessage(db.Model):

    __tablename__ = "contact"
    id = db.Column(db.Integer, primary_key=True)
    created_date = db.Column(db.DateTime, default=datetime.now())
    name = db.Column(db.String(100))
    email = db.Column(db.String(100))
    message = db.Column(db.String(1024))

    def insert(self):
        db.session.add(self)
        db.session.commit()

    def update(self):
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

🔩 Migrations

Note

You can run

shopyo <command>

or

python manage.py <command>

In case of change to models, do

python manage.py db migrate
python manage.py db upgrade