'SQLAlchemy check that column entry is in list when inserting
I want to specify a column in my sqlalchemy model to only accept values that are in a predefined list (in the example below ['bar','baz'].
Below is a minimal example of what such a model would look like
class Foo(db.Model):
"""Example Model"""
# simple autoincrement primary key
id: int = db.Column(db.Integer, primary_key=True, autoincrement=True)
# this column should be constrained to ['bar', 'baz']
exclusive_str: str = db.Column(db.String, nullable=False)
I found the check constraint in the official documentation, but the examples listed are super simple and don't seem to cover a use case like the above. How do I go about solving this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
