我正在尝试将SQL转换为SQLAlchemy.我想要的查询的SQL版本如下:
SELECT * from calendarEventAttendee
JOIN calendarEventAttendanceActual ON calendarEventAttendanceActual.id = calendarEventAttendee.attendanceActualId
LEFT JOIN
(SELECT bill.id, bill.personId, billToEvent.eventId FROM bill JOIN billToEvent ON bill.id = billToEvent.billId) b
ON b.eventId = calendarEventAttendee.eventId AND b.personId = calendarEventAttendee.personId
WHERE b.id is NULL
Run Code Online (Sandbox Code Playgroud)
我的SQLAlchemy查询如下:
query = db.session.query(CalendarEventAttendee).join(CalendarEventAttendanceActual)
sub_query = db.session.query(Bill, BillToEvent).join(BillToEvent, BillToEvent.billId == Bill.id).subquery()
query = query.outerjoin(sub_query, and_(sub_query.Bill.personId == CalendarEventAttendee.personId, Bill.eventId == CalendarEventAttendee.eventId))
results = query.all()
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误 AttributeError: 'Alias' object has no attribute 'Bill'
如果我将SQLAlchemy查询调整为以下内容:
sub_query = db.session.query(Bill, BillToEvent).join(BillToEvent, BillToEvent.billId == Bill.id).subquery()
query = query.outerjoin(sub_query, …Run Code Online (Sandbox Code Playgroud) 我正在学习SQLAlchemy而且我被困住了.我有一个SQL表(table1)有两个字段:'name'和'other_names'
我有一个包含两列的excel文件:
first_name alias
paul patrick
john joe
simon simone
john joey
john jo
Run Code Online (Sandbox Code Playgroud)
我想将excel文件读入我的table1,所以它看起来像这样(即同一行的所有别名都在一行):
paul patrick
john joe,joey,jo
simon simone
Run Code Online (Sandbox Code Playgroud)
这是我试图做的想法.我试过的代码(带注释):
for line in open('file.txt', 'r'): #for each line in the excel file
line = line.strip().split('\t') #split each line with a name and alias
first_name = line[0] #first name is the name before the tab
alias = line[1] #alias is the name after the tab
instance =
Session.query(session,tbs['table1'].name).filter_by(name=first_name) #look through the database table, by name field, and see if …Run Code Online (Sandbox Code Playgroud)