In Flask-SQLAlchemy, models are Python classes that represent database tables. Below is a step-by-step guide on how to create and define models.
- Models in Flask-SQLAlchemy inherit from
db.Model. - Each attribute of the class corresponds to a column in the database table.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return f'<User {self.username}>'
if __name__ == '__main__':
db.create_all()
print("Database and tables created!")- All models inherit from
db.Modelto connect them to the SQLAlchemy instance.
- Define the fields of the table.
- Each column has a data type and optional constraints.
db.Integer: Integer values.db.String: Variable-length string.db.Float: Floating-point numbers.db.Boolean: Boolean values.db.DateTime: Date and time values.db.Text: Large text fields.
primary_key=True: Marks the column as the primary key.unique=True: Ensures unique values for the column.nullable=False: Makes the column non-nullable.
To create the database and tables, use:
if __name__ == '__main__':
db.create_all()This will generate the necessary tables in the database.
This is a basic example of setting up models in Flask-SQLAlchemy. In the next guide, we will cover column attributes in detail, including default values, indexing, and relationships between tables.