diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c96..cfda88bb4 100644 Binary files a/lib/freebies.db and b/lib/freebies.db differ diff --git a/lib/migrations/versions/12e945c366c2_create_freebies_table.py b/lib/migrations/versions/12e945c366c2_create_freebies_table.py new file mode 100644 index 000000000..4f2674f58 --- /dev/null +++ b/lib/migrations/versions/12e945c366c2_create_freebies_table.py @@ -0,0 +1,30 @@ +"""create freebies table + +Revision ID: 12e945c366c2 +Revises: 5f72c58bf48c +Create Date: 2024-09-20 19:57:19.175977 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '12e945c366c2' +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'), nullable=False), + sa.Column('company_id', sa.Integer, sa.ForeignKey('companies.id'), nullable=False), + ) + +def downgrade(): + op.drop_table('freebies') diff --git a/lib/models.py b/lib/models.py index 2681bee5a..f613bb82e 100644 --- a/lib/models.py +++ b/lib/models.py @@ -16,6 +16,9 @@ class Company(Base): name = Column(String()) founding_year = Column(Integer()) + freebies = relationship('Freebie', backref='company') + devs = relationship('Dev', secondary='freebies', backref='companies') + def __repr__(self): return f'' @@ -25,5 +28,19 @@ class Dev(Base): id = Column(Integer(), primary_key=True) name= Column(String()) + freebies = relationship('Freebie', backref='dev') + 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'), nullable=False) + company_id = Column(Integer, ForeignKey('companies.id'), nullable=False) + + def print_details(self): + return f"{self.dev.name} owns a {self.item_name} from {self.company.name}" \ No newline at end of file diff --git a/lib/seed.py b/lib/seed.py index b16becbbb..de49948c3 100644 --- a/lib/seed.py +++ b/lib/seed.py @@ -1,3 +1,23 @@ #!/usr/bin/env python3 # Script goes here! +from models import Company, Dev, Freebie # Ensure these imports match your structure +from base import session # Ensure session is correctly imported + +# Create some companies +company1 = Company(name="Tech Corp", founding_year=2000) +company2 = Company(name="Innovate Ltd", founding_year=1995) + +# Create some developers +dev1 = Dev(name="Alice") +dev2 = Dev(name="Bob") + +# Create some freebies +freebie1 = Freebie(item_name="Mug", value=10, dev=dev1, company=company1) +freebie2 = Freebie(item_name="T-Shirt", value=20, dev=dev2, company=company2) + +# Add and commit everything to the database +session.add_all([company1, company2, dev1, dev2, freebie1, freebie2]) +session.commit() + +