Bird
Raised Fist0
Flaskframework~5 mins

Column types and constraints in Flask - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a column type in Flask-SQLAlchemy?
A column type defines the kind of data a column can hold, like Integer, String, or DateTime. It tells the database what type of information to expect.
Click to reveal answer
beginner
What does the nullable=False constraint do in a column?
It means the column cannot have empty or NULL values. Every row must have a value for that column.
Click to reveal answer
beginner
How do you set a column to be a primary key in Flask-SQLAlchemy?
You add primary_key=True to the column definition. This makes the column unique and identifies each row.
Click to reveal answer
beginner
What is the purpose of the unique=True constraint?
It ensures that all values in the column are different. No two rows can have the same value in that column.
Click to reveal answer
beginner
Name three common column types used in Flask-SQLAlchemy.
Common types include Integer for numbers, String for text, and DateTime for date and time values.
Click to reveal answer
Which column type would you use to store a person's name in Flask-SQLAlchemy?
AString
BInteger
CBoolean
DDateTime
What does primary_key=True do in a column definition?
AAllows NULL values
BSets a default value
CMakes the column optional
DMarks the column as unique and identifies each row
If you want to make sure no two users have the same email, which constraint should you use?
Anullable=False
Bunique=True
Cprimary_key=True
Ddefault='user@example.com'
What happens if you set nullable=False on a column?
AThe column is ignored by the database
BThe column can have empty values
CThe column must always have a value
DThe column automatically increments
Which of these is NOT a valid column type in Flask-SQLAlchemy?
AArrayList
BString
CInteger
DFloat
Explain how to define a column with a type and constraints in Flask-SQLAlchemy.
Think about how you tell the database what kind of data to expect and rules for that data.
You got /3 concepts.
    Why are constraints like unique and nullable important when designing database columns?
    Consider how rules help keep your data clean and trustworthy.
    You got /4 concepts.

      Practice

      (1/5)
      1. Which SQLAlchemy column type should you use in Flask to store text data of variable length?
      easy
      A. db.String
      B. db.Integer
      C. db.Boolean
      D. db.DateTime

      Solution

      1. Step 1: Understand column types for text data

        Text data of variable length is best stored using a string type that allows flexible length.
      2. Step 2: Match the correct SQLAlchemy type

        db.String is designed for variable-length text, unlike db.Integer or db.Boolean.
      3. Final Answer:

        db.String -> Option A
      4. Quick Check:

        Variable-length text = db.String [OK]
      Hint: Text data uses db.String type in Flask models [OK]
      Common Mistakes:
      • Choosing db.Integer for text data
      • Using db.Boolean for strings
      • Confusing db.DateTime with text types
      2. Which of the following is the correct way to declare a non-nullable integer column named age in a Flask model?
      easy
      A. age = db.Column(db.Integer, nullable=True)
      B. age = db.Column(db.Boolean, nullable=False)
      C. age = db.Column(db.String, nullable=False)
      D. age = db.Column(db.Integer, nullable=False)

      Solution

      1. Step 1: Identify the correct type for age

        Age should be stored as an integer, so db.Integer is correct.
      2. Step 2: Set the nullable constraint

        To make the column non-nullable, use nullable=False.
      3. Final Answer:

        age = db.Column(db.Integer, nullable=False) -> Option D
      4. Quick Check:

        Integer + non-nullable = age = db.Column(db.Integer, nullable=False) [OK]
      Hint: Use nullable=False to prevent empty values [OK]
      Common Mistakes:
      • Setting nullable=True when non-null is needed
      • Using wrong data type like String for age
      • Confusing Boolean with Integer type
      3. Given this Flask model column declaration:
      email = db.Column(db.String(120), unique=True, nullable=False)

      What does the unique=True constraint do?
      medium
      A. Prevents duplicate email values in the table
      B. Makes the email column optional
      C. Allows multiple rows to have the same email
      D. Automatically converts emails to lowercase

      Solution

      1. Step 1: Understand the unique constraint

        The unique=True constraint ensures no two rows have the same value in this column.
      2. Step 2: Apply this to the email column

        This means duplicate emails are not allowed, keeping each email distinct.
      3. Final Answer:

        Prevents duplicate email values in the table -> Option A
      4. Quick Check:

        unique=True means no duplicates [OK]
      Hint: unique=True means no two rows share the same value [OK]
      Common Mistakes:
      • Thinking unique=True allows duplicates
      • Confusing unique with nullable
      • Assuming unique changes data format
      4. What is wrong with this Flask model column declaration?
      price = db.Column(db.String, nullable=False, unique='yes')
      medium
      A. The nullable argument cannot be used with unique
      B. The unique argument should be a boolean, not a string
      C. The column type db.String cannot be used for price
      D. The column name price is a reserved keyword

      Solution

      1. Step 1: Check the unique argument type

        The unique parameter expects a boolean value (True or False), not a string.
      2. Step 2: Identify the error in the code

        Passing unique='yes' is invalid and will cause an error.
      3. Final Answer:

        The unique argument should be a boolean, not a string -> Option B
      4. Quick Check:

        unique must be True or False, not 'yes' [OK]
      Hint: unique must be True/False, never a string [OK]
      Common Mistakes:
      • Using string values for boolean parameters
      • Thinking nullable and unique conflict
      • Assuming price is reserved keyword
      5. You want to create a Flask model column for a user's phone number that must be exactly 10 digits, unique, and cannot be empty. Which declaration is best?
      hard
      A. phone = db.Column(db.String, unique=False, nullable=False)
      B. phone = db.Column(db.Integer, unique=True, nullable=False)
      C. phone = db.Column(db.String(10), unique=True, nullable=False)
      D. phone = db.Column(db.String(15), nullable=True)

      Solution

      1. Step 1: Choose the correct type for phone numbers

        Phone numbers often contain leading zeros, so db.String(10) is better than db.Integer.
      2. Step 2: Apply constraints for uniqueness and non-null

        Use unique=True to prevent duplicates and nullable=False to disallow empty values.
      3. Final Answer:

        phone = db.Column(db.String(10), unique=True, nullable=False) -> Option C
      4. Quick Check:

        String(10) + unique + non-null = phone = db.Column(db.String(10), unique=True, nullable=False) [OK]
      Hint: Use String with length, unique=True, nullable=False for fixed phone numbers [OK]
      Common Mistakes:
      • Using Integer type losing leading zeros
      • Allowing nullable=True when value is required
      • Ignoring unique constraint for phone numbers
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard