-
-
Save TheMuellenator/b4e21f77c04afcac85a7ec2a262ea0d7 to your computer and use it in GitHub Desktop.
Day 69 L532 Challenge 1 - Creating Relational Databases (SQL Alchemy 3.1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## MORE CODE ABOVE | |
class User(UserMixin, db.Model): | |
__tablename__ = "users" | |
id: Mapped[int] = mapped_column(Integer, primary_key=True) | |
email: Mapped[str] = mapped_column(String(100), unique=True) | |
password: Mapped[str] = mapped_column(String(100)) | |
name: Mapped[str] = mapped_column(String(100)) | |
#This will act like a List of BlogPost objects attached to each User. | |
#The "author" refers to the author property in the BlogPost class. | |
posts = relationship("BlogPost", back_populates="author") | |
class BlogPost(db.Model): | |
__tablename__ = "blog_posts" | |
id: Mapped[int] = mapped_column(Integer, primary_key=True) | |
# Create Foreign Key, "users.id" the users refers to the tablename of User. | |
author_id: Mapped[int] = mapped_column(Integer, db.ForeignKey("users.id")) | |
# Create reference to the User object. The "posts" refers to the posts property in the User class. | |
author = relationship("User", back_populates="posts") | |
title: Mapped[str] = mapped_column(String(250), unique=True, nullable=False) | |
subtitle: Mapped[str] = mapped_column(String(250), nullable=False) | |
date: Mapped[str] = mapped_column(String(250), nullable=False) | |
body: Mapped[str] = mapped_column(Text, nullable=False) | |
img_url: Mapped[str] = mapped_column(String(250), nullable=False) | |
## MORE CODE BELOW |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx, it works. It takes a lot of time to comment all current_user and other jinja variables, but it done.