From 6749aa750c116c4393942fe1d9ff520c18815d50 Mon Sep 17 00:00:00 2001 From: HABIBA HASSAN GUYO Date: Tue, 4 Mar 2025 08:44:38 +0300 Subject: [PATCH 1/2] Complete labs --- lib/debug.py | 10 ++-- lib/freebies.db | Bin 20480 -> 24576 bytes .../95122781f9f6_create_freebies_table.py | 30 ++++++++++++ lib/models.py | 43 +++++++++++++++--- lib/seed.py | 29 +++++++++++- 5 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 lib/migrations/versions/95122781f9f6_create_freebies_table.py diff --git a/lib/debug.py b/lib/debug.py index 4f922eb69..be8155e5a 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,9 +1,13 @@ #!/usr/bin/env python3 from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker -from models import Company, Dev +from models import Company, Dev, Freebie # Import all models if __name__ == '__main__': - engine = create_engine('sqlite:///freebies.db') - import ipdb; ipdb.set_trace() + engine = create_engine('sqlite:///freebies.db') # Create engine + Session = sessionmaker(bind=engine) + session = Session() # Create session + + import ipdb; ipdb.set_trace() # Start interactive debugging diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c963e832db481e7a7493e3029e691ac4dc..dcf0ef69baee23aad91ed3b56e9bdea765bf1e82 100644 GIT binary patch delta 644 zcmZozz}Rqrae}lUCj$cm8xX?)%S0VxaZUz3`2e~?ixGID=I{CONKt(4z^2vl`mZav! z=OyN*Dug)(IeRz;Dfsz^DENi?_~w{z0yu?tTj1u8|rLaZQCF zS2x!nS3hUhUWae}lUD+2=q2*UvLL>*&MRtCLzSzi7h3@m(74E((OJNfGQq&5o*EaTlQ r#dk}P$285{DB08^Db2(pd6U8yej#K5UXW1&K-Ks8Cr*%NL*)VhkJ1_o diff --git a/lib/migrations/versions/95122781f9f6_create_freebies_table.py b/lib/migrations/versions/95122781f9f6_create_freebies_table.py new file mode 100644 index 000000000..7c5916f3a --- /dev/null +++ b/lib/migrations/versions/95122781f9f6_create_freebies_table.py @@ -0,0 +1,30 @@ +"""Create freebies table + +Revision ID: 95122781f9f6 +Revises: 5f72c58bf48c +Create Date: 2025-03-04 08:02:07.008788 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '95122781f9f6' +down_revision = '5f72c58bf48c' +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'freebies', + sa.Column('id', sa.Integer, primary_key=True), + sa.Column('item_name', sa.String, nullable=False), + sa.Column('value', sa.Integer, nullable=False), + sa.Column('dev_id', sa.Integer, sa.ForeignKey('devs.id')), + sa.Column('company_id', sa.Integer, sa.ForeignKey('companies.id')) + ) + +def downgrade(): + op.drop_table('freebies') diff --git a/lib/models.py b/lib/models.py index 2681bee5a..39af93a0f 100644 --- a/lib/models.py +++ b/lib/models.py @@ -1,7 +1,8 @@ from sqlalchemy import ForeignKey, Column, Integer, String, MetaData -from sqlalchemy.orm import relationship, backref +from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base +# Define naming conventions for foreign keys convention = { "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", } @@ -12,9 +13,15 @@ class Company(Base): __tablename__ = 'companies' - id = Column(Integer(), primary_key=True) - name = Column(String()) - founding_year = Column(Integer()) + id = Column(Integer, primary_key=True) + name = Column(String, nullable=False) + founding_year = Column(Integer, nullable=False) + + # Define relationship to Freebie + freebies = relationship("Freebie", back_populates="company") + + # Define relationship to Dev through Freebie (Add overlaps to avoid conflict) + devs = relationship("Dev", secondary="freebies", back_populates="companies", overlaps="freebies") def __repr__(self): return f'' @@ -22,8 +29,30 @@ def __repr__(self): class Dev(Base): __tablename__ = 'devs' - id = Column(Integer(), primary_key=True) - name= Column(String()) - + id = Column(Integer, primary_key=True) + name = Column(String, nullable=False) + + # Define relationship to Freebie + freebies = relationship("Freebie", back_populates="dev") + + # Define relationship to Company through Freebie (Add overlaps to avoid conflict) + companies = relationship("Company", secondary="freebies", back_populates="devs", overlaps="freebies") + def __repr__(self): return f'' + +class Freebie(Base): + __tablename__ = 'freebies' + + id = Column(Integer, primary_key=True) + item_name = Column(String, nullable=False) + value = Column(Integer, nullable=False) + dev_id = Column(Integer, ForeignKey('devs.id')) + company_id = Column(Integer, ForeignKey('companies.id')) + + # Define relationships with overlaps to avoid conflicts + dev = relationship("Dev", back_populates="freebies", overlaps="companies,devs") + company = relationship("Company", back_populates="freebies", overlaps="companies,devs") + + def __repr__(self): + return f'' diff --git a/lib/seed.py b/lib/seed.py index b16becbbb..80d9ad983 100644 --- a/lib/seed.py +++ b/lib/seed.py @@ -1,3 +1,28 @@ -#!/usr/bin/env python3 +from sqlalchemy.orm import sessionmaker +from sqlalchemy import create_engine +from models import Dev, Company, Freebie, Base # Ensure you import Base for table creation -# Script goes here! +# Create an engine (modify the database URL as needed) +engine = create_engine('sqlite:///freebies.db') # Or use another DB type + +# Create session factory and initialize a session instance +Session = sessionmaker(bind=engine) +session = Session() # This is the actual session object + +# Ensure the database tables are created +Base.metadata.create_all(engine) + +# Seed data +dev1 = Dev(name="Alice") +dev2 = Dev(name="Bob") +company1 = Company(name="Google", founding_year=1998) +company2 = Company(name="Microsoft", founding_year=1975) + +freebie1 = Freebie(item_name="T-Shirt", value=10, dev=dev1, company=company1) +freebie2 = Freebie(item_name="Mug", value=5, dev=dev2, company=company2) + +# Add records to the session and commit +session.add_all([dev1, dev2, company1, company2, freebie1, freebie2]) +session.commit() + +print("Database seeded successfully!") From e4d45ab12fcc360dbf80543767068738730e75d3 Mon Sep 17 00:00:00 2001 From: HABIBA HASSAN GUYO Date: Wed, 12 Mar 2025 13:48:11 +0300 Subject: [PATCH 2/2] complete labs --- lib/__pycache__/models.cpython-38.pyc | Bin 0 -> 2100 bytes lib/freebies.db | Bin 24576 -> 24576 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/__pycache__/models.cpython-38.pyc diff --git a/lib/__pycache__/models.cpython-38.pyc b/lib/__pycache__/models.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b2977a03bc185c0b871cf0975c5a1ba1387411a9 GIT binary patch literal 2100 zcmb7FUvJ|?5ck?n{3lMDD?_?L;`@ zt@m2K0J;y*FT#h|SDyF^_X0EPB=vzR6-P6f-|l*MW_~m4AG%#v!>9V~kMsMsru|8v z`IrcfP>N@$xW-wmCCq1u?(2!+8;R+gNy~3Bn$zQUV)>S`jo3~c-$C2tt%&(9w7})< zOXhdLfeyFO^1jlz&7Ef&cfh+a{4QdbcMx|J?jiPg7jakN9o~Da4R`)RPqbl6x~G|d z=sf)fD%n2H;zg25>+7_Ha}cuqxD-)(F75B23{S!`95U$%h{H0<(qb0ni2EESaVT(T z3BfolKWk?-c(7ZB;~0W8Okh|boT%-CPp?M=OhE|XL31T;dI#UZgM&$yE+M7U zaL`WB3yxk)$oJ;cMsS2u{DcZLpK;CCIr9xLp~ZD>T3enN{BE5yX&q-t9;TIaCd!DQcyh&LCl^r?iYk~xb&A+d7jaCXOY2ks zj3YGa7b=|Xjl;=2$g_Nbd4xh5C$NFMqVR_HVBg=sk#09F0dw#vv~D^Ge!5ARre(aF@K% zQE6<|U!T6enLHczXeD_O1kw!x++cwP5=buyo-V?8J<|>9dZ?kGCIb=D>2)g>j~AdhxYJrP9Wsp!+dE2yM_VTjiVlE*4E@fKSDw+vbbSYZS4 zHf?f`)VrhxTcwgh*&3p3$n)@>260{SKCS^;pk9(N%k# z4g1n9p2lH3nL$#iLmLIDYLUJRvUj~`KNE>^!oS-XgkQ@2#z#!x7hIF*_U6+gIy7K@ ui8o^1iK=H+9Sm@$5dk?b?SuMSJ0w~Q>fd{GznHqF|7{ws?&|%1f9D?(^v~A- literal 0 HcmV?d00001 diff --git a/lib/freebies.db b/lib/freebies.db index dcf0ef69baee23aad91ed3b56e9bdea765bf1e82..8c2ecef29083533a52ca65bf061a694e33ae330f 100644 GIT binary patch delta 181 zcmZoTz}Rqrae_1>-$WT_M!t;+OY{ZU_!l$qf8u}0f0_Rf{}%qmn*{~>`Q`c9SQzA) zd^3}a@{9A+O4zsau(B{nGP&pHr{|=ypPPKh-U+Dl6a)V^{&)OO`0wyv;y(q{xs6|l zjg6T>*eO4WgO!;<+%YFJId$?Ke+{7GMGX8O_#g3K09vzwe-Th|AHM<*8ykZ#BO{}4 eX*w$#n*b{tgEWvAq8pr%SyaNs$~yT_yd3}+S~6w; delta 85 zcmZoTz}Rqrae_1>=R_H2M$U~1OZ0hI_zyAgf8u}0f0_T#W}4FBXi{u&UqANU{fUx2HPw*vs>u^xT^