From 738d242691fa924de599cdcbd5324ff8152f6e7f Mon Sep 17 00:00:00 2001 From: "[esekyi]" <[sskert10@gmail.com]> Date: Sun, 25 Aug 2024 22:53:50 +0000 Subject: [PATCH] =?UTF-8?q?[Fixes=F0=9F=A6=BE]=20Add=20db=20Column=20UUID?= =?UTF-8?q?=20to=20id=20fields.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/category.py | 4 ++-- app/models/comment.py | 11 ++++++----- app/models/ingredient.py | 7 ++++--- app/models/recipe.py | 9 +++++---- app/models/user.py | 9 ++++++--- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/app/models/category.py b/app/models/category.py index b5aebb0..fa1a9d7 100644 --- a/app/models/category.py +++ b/app/models/category.py @@ -1,11 +1,11 @@ from app import db from datetime import datetime import uuid +from sqlalchemy.dialects.postgresql import UUID class Category(db.Model): - id = db.Column(db.String(36), primary_key=True, - default=lambda: str(uuid.uuid4())) + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = db.Column(db.String(80), unique=True, nullable=False) description = db.Column(db.Text) created_at = db.Column(db.DateTime, default=datetime.utcnow) diff --git a/app/models/comment.py b/app/models/comment.py index 4fe2baf..ea562cb 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -1,15 +1,16 @@ from app import db from datetime import datetime import uuid +from sqlalchemy.dialects.postgresql import UUID class Comment(db.Model): - id = db.Column(db.String(36), primary_key=True, - default=lambda: str(uuid.uuid4())) + id = db.Column(UUID(as_uuid=True), primary_key=True, + default=uuid.uuid4) text = db.Column(db.Text, nullable=False) - user_id = db.Column(db.String(36), db.ForeignKey( - 'user.id'), nullable=False) - recipe_id = db.Column(db.String(36), db.ForeignKey( + user_id = db.Column(UUID(as_uuid=True), + db.ForeignKey('user.id'), nullable=False) + recipe_id = db.Column(UUID(as_uuid=True), db.ForeignKey( 'recipe.id'), nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column( diff --git a/app/models/ingredient.py b/app/models/ingredient.py index b7790da..0c09b9f 100644 --- a/app/models/ingredient.py +++ b/app/models/ingredient.py @@ -1,14 +1,15 @@ from app import db from datetime import datetime import uuid +from sqlalchemy.dialects.postgresql import UUID class Ingredient(db.Model): - id = db.Column(db.String(36), primary_key=True, - default=lambda: str(uuid.uuid4())) + id = db.Column(UUID(as_uuid=True), primary_key=True, + default=uuid.uuid4) name = db.Column(db.String(120), nullable=False) quantity = db.Column(db.String(50), nullable=False) - recipe_id = db.Column(db.String(36), db.ForeignKey( + recipe_id = db.Column(UUID(as_uuid=True), db.ForeignKey( 'recipe.id'), nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column( diff --git a/app/models/recipe.py b/app/models/recipe.py index 72b6b03..1eb8de9 100644 --- a/app/models/recipe.py +++ b/app/models/recipe.py @@ -1,11 +1,12 @@ from app import db from datetime import datetime import uuid +from sqlalchemy.dialects.postgresql import UUID class Recipe(db.Model): - id = db.Column(db.String(36), primary_key=True, - default=lambda: str(uuid.uuid4())) + id = db.Column(UUID(as_uuid=True), primary_key=True, + default=uuid.uuid4) title = db.Column(db.String(120), nullable=False) description = db.Column(db.Text, nullable=False) instructions = db.Column(db.Text, nullable=False) @@ -13,9 +14,9 @@ class Recipe(db.Model): cook_time = db.Column(db.Integer) # in minutes servings = db.Column(db.Integer) image_url = db.Column(db.String(255)) - category_id = db.Column(db.String(36), db.ForeignKey( + category_id = db.Column(UUID(as_uuid=True), db.ForeignKey( 'category.id'), nullable=False) - user_id = db.Column(db.String(36), db.ForeignKey( + user_id = db.Column(UUID(as_uuid=True), db.ForeignKey( 'user.id'), nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column( diff --git a/app/models/user.py b/app/models/user.py index bff3cc9..09a10bb 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -2,14 +2,17 @@ from datetime import datetime import uuid from flask_login import UserMixin +from sqlalchemy.dialects.postgresql import UUID class User(db.Model, UserMixin): - id = db.Column(db.String(36), primary_key=True, - default=lambda: str(uuid.uuid4())) + id = db.Column(UUID(as_uuid=True), primary_key=True, + default = uuid.uuid4) + first_name = db.Column(db.String(50), nullable=False) + last_name = db.Column(db.String(50), nullable=False) username = db.Column(db.String(80), unique=True, nullable=False, index=True) email = db.Column(db.String(120), unique=True, nullable=False, index=True) - password_hash = db.Column(db.String(128), nullable=False) + password_hash = db.Column(db.String(255), nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column( db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)