Recall & Review
beginner
What is a model in Flask's context?
A model in Flask represents the structure of data in your application. It defines how data is stored, accessed, and related, usually using classes that map to database tables.
Click to reveal answer
beginner
Which Flask extension is commonly used for defining models?
Flask-SQLAlchemy is the common extension used to define models. It provides tools to create classes that represent database tables and manage database operations easily.
Click to reveal answer
beginner
What does this code snippet define?
<pre>class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)</pre>This defines a User model with two fields: 'id' as a unique identifier (primary key) and 'name' as a required text field with a maximum length of 50 characters.
Click to reveal answer
beginner
Why do we use 'primary_key=True' in a model field?
Setting 'primary_key=True' marks that field as the unique identifier for each record in the database table. It helps to find, update, or delete specific records easily.
Click to reveal answer
beginner
What does 'nullable=False' mean in a model field?
'nullable=False' means that the field cannot be empty. The database will require a value for this field when creating or updating a record.
Click to reveal answer
In Flask-SQLAlchemy, what does 'db.Model' represent?
✗ Incorrect
'db.Model' is the base class that all model classes inherit from to represent database tables.
Which of these is a valid field type in Flask-SQLAlchemy models?
✗ Incorrect
'db.Integer' is a valid field type. The correct string type is 'db.String', not 'db.StringField'.
What happens if you omit 'primary_key=True' in a model?
✗ Incorrect
Without a primary key, the model lacks a unique identifier, which is essential for database operations.
What does 'db.Column(db.String(100), nullable=True)' mean?
✗ Incorrect
'nullable=True' means the field can be empty, and 'db.String(100)' limits text length to 100 characters.
How do you define a model named 'Product' with an integer 'id' primary key in Flask-SQLAlchemy?
✗ Incorrect
Models inherit from 'db.Model' and define fields with 'db.Column'. The 'id' field needs 'primary_key=True'.
Explain how to define a simple model in Flask using Flask-SQLAlchemy.
Think about how a class maps to a database table and fields map to columns.
You got /5 concepts.
Describe the role of 'primary_key' and 'nullable' in model field definitions.
Consider how databases identify records and enforce data rules.
You got /3 concepts.