Declaring models with SQLAlchemy
1
2
3
4
5
6
7
8
9
10
11
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
addresses = db.relationship('Address', backref='person', lazy=True)
class Address(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(120), nullable=False)
person_id = db.Column(db.Integer, db.ForeignKey('person.id'),
nullable=False)
Commands to get ready and run local environment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Run app in debug mode:
$ flask --app main --debug run
# Start migrations
$ flask --app main db init
# make migrations
$ flask --app main db migrate
# migrate changes
$ flask --app main db upgrade
# Run debug server
$ flask --app main --debug run
# Run shell
$ flask --app main shell
# Deleting from a shell:
>>> from models import db, MLProject
>>> MLProject.query.delete()
>>> db.session.commit()