Flask_SQLAlchemy is claiming my value is not boolean
Step 1: Define the Model
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)
active = db.Column(db.Boolean)
Step 2: Create an Instance
user = User(username='example', active='yes')
Step 3: Add and Commit the Instance
db.session.add(user)
db.session.commit()
Explanation for Step 1:
In the first step, we define the model using Flask-SQLAlchemy. We create an instance of the SQLAlchemy
class and define a User
model class that inherits from db.Model
. Within the User
class, we define three columns: id
as an integer primary key, username
as a string with a maximum length of 80 characters, and active
as a boolean.
Explanation for Step 2:
In the second step, we create an instance of the User
model, setting the username
to 'example' and the active
attribute to 'yes'. However, the issue here is that the active
attribute is expecting a boolean value, but we are providing a string 'yes' instead.
Explanation for Step 3:
In the third step, we add the user
instance to the session and commit the changes to the database. However, since the active
attribute is expecting a boolean value, the attempted insertion of the 'yes' string will result in a validation error, as it does not correspond to a boolean value.