Git Product home page Git Product logo

python-p4-validation-constraints's Introduction

SQLAlchemy Constraints

Learning Goals

  • Define constraints and validations in data processing.
  • Restrict database input to specific formats using constraints.

Key Vocab

  • Constraint: a rule enforced on the data columns of a table. Ensures that only appropriate data is saved to the database.
  • Validation: an automatic check to ensure that data entered is sensible and feasible.
  • Forms: A web form (or HTML form) is a place where users enter data or personal information that's then sent to a server for processing.

Introduction

Invalid values in our database can cause all kinds of problems when we want to use the data. To keep invalid values out of our database we can use SQLAlchemy constraints. These constraints work at a database level to control the data we add to the database.


unique and nullable Constraints

Certain constraints in SQLAlchemy can be added directly to the column as we define it. The nullable constraint allows us to make sure the values added to the columns are not null. We can define this by adding the argument nullable=False to the Column constructor.

If it's required that the values in your columns must be unique then the unique constraint can be specified by adding the argument unique=True to the Column constructor.


CheckConstraint

CheckConstraints can be created for columns and tables. The text of the CheckConstraint is passed directly through to the database. Some databases like MySQL do not support CheckConstraints.

Here is an example of a Patient table that uses constraints to control input of birth_year and death_year. You can find the complete code in bin/constraints.py.

class Patient(base):
    __tablename__ = 'patient'
    name = Column(String(length=50), primary_key=True)
    # Constraint defined at the Column level
    birth_year = Column(Integer,
                        CheckConstraint('birth_year < 2023'),
                        nullable=False)
    death_year = Column(Integer)
    # Constraint defined at the Table level
    __table_args__ = (
        CheckConstraint('(death_year is NULL) or (death_year >= birth_year)'),
    )

Here we have a column CheckConstraint which makes sure we cannot add a birth_year that is greater than 2023. We also have a table CheckConstraint that makes sure the death year is after the birth year or the death year is null (meaning that the patient is still alive).


Conclusion

SQLAlchemy constraints allow us to control input at a database level. They allow us to use any valid SQL and numeric comparisons to validate input. In the next lesson we will learn how to control input at a Python ORM level.


Resources

python-p4-validation-constraints's People

Contributors

pgill97 avatar professor-ben avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.