11 3 2 Models and Databases Explained
Key Concepts
Models and Databases in web development involve several key concepts:
- Introduction to Models
- Database Integration
- ORM (Object-Relational Mapping)
- Creating Models
- Database Queries
- Relationships in Databases
1. Introduction to Models
Models in web development represent the data structure and business logic of an application. They define how data is stored, retrieved, and manipulated.
2. Database Integration
Database integration involves connecting your web application to a database to store and retrieve data. Common databases include SQLite, MySQL, and PostgreSQL.
Example:
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' db = SQLAlchemy(app)
Analogy: Think of a database as a warehouse where you store and retrieve items, and the integration is the process of setting up a conveyor belt to move items in and out.
3. ORM (Object-Relational Mapping)
ORM is a technique that maps database records to objects in your code. This allows you to interact with the database using object-oriented programming.
Example:
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() 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)
Analogy: Think of ORM as a translator that converts spoken language into written text, allowing you to communicate with the database in a language you understand.
4. Creating Models
Creating models involves defining the structure of your data using classes. Each class represents a table in the database, and its attributes represent columns.
Example:
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
Analogy: Think of creating models as designing the blueprint for a house, where each room represents a table and its features represent columns.
5. Database Queries
Database queries are used to retrieve, insert, update, and delete data from the database. ORM frameworks provide methods to perform these operations.
Example:
# Retrieve all users users = User.query.all() # Retrieve a user by username user = User.query.filter_by(username='john_doe').first() # Insert a new user new_user = User(username='jane_doe', email='jane@example.com') db.session.add(new_user) db.session.commit()
Analogy: Think of database queries as asking questions to a librarian to find a specific book, where each question retrieves a different piece of information.
6. Relationships in Databases
Relationships in databases define how tables are connected. Common relationships include one-to-one, one-to-many, and many-to-many.
Example:
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) posts = db.relationship('Post', backref='author', lazy=True) class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
Analogy: Think of relationships in databases as family ties, where each member is connected to others in specific ways, such as parent-child or sibling relationships.