I used SQLAlchemy to create a SQLite database which stores bibliographic data of some document, and I want to query the author number of each document.
I know how to do this in raw SQL, but how can I achieve the same result using SQLAlchemy? It is possible without using join
?
Here is the classes that I have defined:
class WosDocument(Base): __tablename__ = 'wos_document' document_id = Column(Integer, primary_key=True) unique_id = Column(String, unique=True) ...... authors = relationship('WosAuthor', back_populates='document') class WosAuthor(Base): __tablename__ = 'wos_author' author_id = Column(Integer, primary_key=True, autoincrement=True) document_unique_id = Column(String, ForeignKey('wos_document.unique_id')) document = relationship('WosDocument', back_populates='authors') last_name = Column(String) first_name = Column(String)
And my goal is to get the same result as this SQL query does:
SELECT a.unique_id, COUNT(*) FROM wos_document AS a LEFT JOIN wos_author AS b ON a.unique_id = b.document_unique_id GROUP BY a.unique_id
I tried the codes below:
session.query(WosDocument.unique_id, len(WosDocument.authors)).all() session.query(WosDocument.unique_id, func.count(WosDocument.authors)).all()
The first line raised an error, the second line doesn't give me the desired result, it return only one row and I don't recognize what it is:
[('000275510800023', 40685268)]
Since WosDocument
Object has a one-to-many relationship authors
, I supposed that I can query the author number of each document without using join
explicitly, but I can't find out how to do this with SQLAlchemy.
Can you help me? Thanks!
document = db.relationship( 'Wos', primaryjoin='WosAuther.document_unique_id == WosDocument.document_id', backref='authers')
you can query document, and it would have a list contain many wosauther – Shihe Zhangwosdocument.authors
with my relationship, but I don't know how to query the author number count of each document. – Tom Leunglen(wosdocument.authors )
? – Shihe ZhangSELECT a.unique_id, COUNT(*) FROM wos_document AS a LEFT JOIN wos_author AS b ON a.unique_id = b.document_unique_id GROUP BY a.unique_id
This query result puzzle me. if some document have no author the count(*) would still have 1.Are you sure that's what you want? – Shihe Zhanglen(wosdocument.authors)
will raise an error if I put it into session.query(). But thelen()
function works fine if I already fetched a document object – Tom Leung