class Ingredient(db.Model, SerializerMixin): __tablename__ = 'Ingredient' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False, unique=True) recipes = db.relationship('Recipe', secondary='RecipeIngredient') serialize_only = ('id', 'name', 'recipes.id') def __repr__(self): return '<Ingredient %r>' % self.name
class RecipeStep(db.Model, SerializerMixin): __tablename__ = 'RecipeStep' id = db.Column(db.Integer, primary_key=True) number = db.Column(db.Integer, nullable=False) content = db.Column(db.String, nullable=False) recipe_id = db.Column(db.Integer, db.ForeignKey('Recipe.id')) recipe = db.relationship('Recipe') serialize_only = ('id', 'number', 'content') def __repr__(self): return '<Step %r>' % self.number
class User(db.Model, SerializerMixin): __tablename__ = 'User' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(35), unique=True, nullable=False) name = db.Column(db.String(25), unique=True, nullable=False) # NO SPACES! password = db.Column(db.String(64), nullable=False) joined_on = db.Column(db.Date, nullable=False) recipes = db.relationship("Recipe", backref="User", lazy="dynamic") comments = db.relationship("Comment", backref="User", lazy="dynamic") serialize_only = ('id', 'email', 'name', 'joined_on', 'recipes.id', 'comments.id') def __repr__(self): return '<User %r>' % self.name
class Comment(db.Model, SerializerMixin): __tablename__ = 'Comment' id = db.Column(db.Integer, primary_key=True) content = db.Column(db.String, nullable=False) submitted_on = db.Column(db.DateTime, nullable=False) # Made on a recipe recipe_id = db.Column(db.Integer, db.ForeignKey('Recipe.id')) recipe = db.relationship('Recipe') # Made by a user user_id = db.Column(db.Integer, db.ForeignKey('User.id')) user = db.relationship('User') serialize_only = ('id', 'content', 'submitted_on', 'recipe_id', 'user_id') datetime_format = '%Y-%m-%dT%H:%M:%SZ' def __repr__(self): return '<Comment %r>' % self.id
class RecipeIngredient(db.Model, SerializerMixin): __tablename__ = 'RecipeIngredient' recipe_id = db.Column(db.Integer, db.ForeignKey('Recipe.id'), primary_key=True) ingredient_id = db.Column(db.Integer, db.ForeignKey('Ingredient.id'), primary_key=True) amount = db.Column(db.Float, nullable=False) units = db.Column(db.String) ingredient = db.relationship('Ingredient', backref=db.backref('recipe_assoc')) serialize_only = ('ingredient.name', 'amount', 'units') def __repr__(self): return '<RecipeIngredient %r-%r>' % (self.recipe_id, self.ingredient_id)
class Recipe(db.Model, SerializerMixin): __tablename__ = 'Recipe' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True) prep_time_minutes = db.Column(db.Integer) cook_time_minutes = db.Column(db.Integer) submitted_on = db.Column(db.Date, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('User.id')) user = db.relationship('User') steps = db.relationship("RecipeStep", backref="Recipe", lazy="dynamic", cascade="all,delete") comments = db.relationship("Comment", backref="Recipe", lazy="dynamic", cascade="all,delete") ingredients_raw = db.relationship('Ingredient', secondary='RecipeIngredient') ingredients = db.relationship('RecipeIngredient', backref=db.backref('recipe'), cascade='all,delete', passive_deletes=True) serialize_only = ('id', 'name', 'prep_time_minutes', 'cook_time_minutes', 'submitted_on', 'user_id', 'steps.number', 'steps.content', 'comments.id', 'ingredients') def __repr__(self): return '<Recipe %r>' % self.name