I have 2 tables as provided below which are already populated,
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)
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))
Now, I would like to write an query for get all the Puppies group by their Shelter name using Python. So, it will need to be printed as puppyName puppyShelterName.
I write the code as following,
puppies = session.query(Puppy).group_by(Puppy.shelter_id).all()
for puppy in puppies:
puppy.name
This is obviously not working as I wanted. I would like to print all the puppies name with their shelter name group by in which shelter they are staying. How to correct the code ?
array_agg().sqliteand I initiated as following,engine = create_engine('sqlite:///puppyshelter.db')puppy1 shelter1puppy2 shelter1puppy3 shelter1puppy4 shelter2puppy5 shelter2puppy6 shelter2puppy7 shelter2puppy8 shelter3puppy9 shelter3puppy10 shelter3puppy11 shelter3puppy1 shelter3