Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write pydantic objects to postgres #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ verify_ssl = true
name = "pypi"

[packages]
pynntp = "*"
fastapi = "*"
psycopg2-binary = "*"
pynntp = "*"
sqlalchemy = "*"

[dev-packages]

Expand Down
841 changes: 237 additions & 604 deletions Pipfile.lock

Large diffs are not rendered by default.

Empty file added src/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions src/education_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EducationResource(BaseModel):
of subject tags, a creation date, last modified date, and usage information.
"""

from .pedigree_record import PedigreeRecord
# from pedigree_record import PedigreeRecord

title: str = ""
description: str = ""
Expand All @@ -20,4 +20,4 @@ class EducationResource(BaseModel):
subject_tags: list[str] = []
creation_date: datetime | None = None
last_modified_date: datetime | None = None
usage: list[PedigreeRecord] = []
usage_comments: list[str] = [] # list[PedigreeRecord] = []
2 changes: 1 addition & 1 deletion src/pedigree_record.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from pydantic import BaseModel

from .education_resource import EducationResource
from education_resource import EducationResource


class PedigreeRecord(BaseModel):
Expand Down
50 changes: 50 additions & 0 deletions src/write_pydantic_objects_to_postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# from pydantic import BaseModel
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import declarative_base, sessionmaker

from education_resource import EducationResource
# from .pedigree_record import PedigreeRecord

# Set up SQLAlchemy base and engine
Base = declarative_base()
DATABASE_URL = "postgresql://cclauss:@localhost/mydatabase" # Replace with your actual database details
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


# Define a SQLAlchemy model corresponding to your Pydantic model
class EducationResourceDB(Base):
__tablename__ = "education_resources"

id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)


# Create the database tables
Base.metadata.create_all(bind=engine)


# Function to add a new user to the database
def add_education_resource_to_db(education_resource: EducationResource):
db = SessionLocal()
try:
# Convert the Pydantic user model to SQLAlchemy model
db_education_resource = EducationResourceDB(title=education_resource.title)
db.add(db_education_resource)
db.commit()
db.refresh(db_education_resource)
return db_education_resource
except Exception as e:
db.rollback()
raise e
finally:
db.close()


# Example usage
if __name__ == "__main__":
education_resource = EducationResource(title="2024 Debate")
education_resource_in_db = add_education_resource_to_db(education_resource)
print(
f"EducationResource added to DB: {education_resource.title} (ID: {education_resource.id})"
)