mardi 3 novembre 2015

Python function with SQLAlchemy to count and update table

I have two tables. One table is a table of different puppies:

class Puppy(Base):
    __tablename__ = 'puppy'
    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)
    gender = Column(String(6), nullable=False)
    dateOfBirth = Column(Date)
    picture = Column(String)
    shelter_id = Column(Integer, ForeignKey('shelter.id'))
    shelter = relationship(Shelter)
    weight = Column(Numeric(10))
    profile = relationship("Profile")
    adoption = relationship("Adoption", secondary=association_table)

And the other table is a list of shelters:

class Shelter(Base):
    __tablename__ = 'shelter'
    id = Column(Integer, primary_key=True)
    name = Column(String(80), nullable=False)
    address = Column(String(250))
    city = Column(String(80))
    state = Column(String(20))
    zipCode = Column(String(10))
    website = Column(String)
    current_capacity = Column(Integer)

I would like to create a function that counts how many puppies are in each each shelter, and then can update the "current capacity" in the Shelter table using SQLAlchemy, but I have no idea where to start. I know I need to join the two, but I don't know how to return the results.

Aucun commentaire:

Enregistrer un commentaire