Insert values from a list of dictionaries using SQLAlchemy Postgres in python

I have a table in RDS (aurora postgres) that looks like this:

students:

student_id (pk) student_name number_of_classes_taken one_violation multiple_violation
1234 joe_smith 1 False False
5678 jane_smith 9 True True

I have a list of dictionaries that looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>students = [
{"student_id": "1234", "name": "joe_smith", "classes_added": 1},
{"student_id": "5678", "name": "jane_smith", "classes_added": 3},
{"student_id": "9012", "name": "jenny_smith", "classes_added": 5}
]
</code>
<code>students = [ {"student_id": "1234", "name": "joe_smith", "classes_added": 1}, {"student_id": "5678", "name": "jane_smith", "classes_added": 3}, {"student_id": "9012", "name": "jenny_smith", "classes_added": 5} ] </code>
students = [
{"student_id": "1234", "name": "joe_smith", "classes_added": 1},
{"student_id": "5678", "name": "jane_smith", "classes_added": 3},
{"student_id": "9012", "name": "jenny_smith", "classes_added": 5}
]

I want to insert the students from the list of dictionaries that aren’t already in the table while updating the ones that are. If a student exists, I want to update the column for number_of_classes_taken to be the sum of the existing integer in the row and the integer for classes_added in the dictionary. So for joe_smith, it would be 2 (1 existing + 1 added). For jane_smith it would be 12 (9 existing + 3 added).

For students that aren’t in the table, I just want to insert them and set number_of_classes_taken to the value for classes_added.

Ideally, if this is possible, I would also like to update one_violation to True for anyone taking more than 6 classes and multiple_violation to True for anyone taking more than 7 classes. Otherwise set them both to False. On inserts of new students, I’d like both of these to be set to False by default. If this is not possible in the same query/call, It can be done separately.

So far I’ve tried:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from sqlalchemy.dialects.postgressql import insert
# Iterate the list of students to be added
for student in students:
insert_stmt = insert(students).values(
student_id={student['student_id']},
student_name={student['name']},
number_of_classes_taken={student['classes_added']},
one_violation={False},
multiple_violation={False})
# If a student exists based off the primary key (the student_id) update the row with the sum(classes_added + number_of_classes_taken) and update the one_violation and multiple_violation column if the student has more than 6 or more than 7 classes
do_update_stmt = insert_stmt.on_conflict_do_update(
constraint="student_id_pk",
set_=dict(number_of_classes_taken=student["classes_added"], one_violation=??, multiple_violation=??),
)
</code>
<code>from sqlalchemy.dialects.postgressql import insert # Iterate the list of students to be added for student in students: insert_stmt = insert(students).values( student_id={student['student_id']}, student_name={student['name']}, number_of_classes_taken={student['classes_added']}, one_violation={False}, multiple_violation={False}) # If a student exists based off the primary key (the student_id) update the row with the sum(classes_added + number_of_classes_taken) and update the one_violation and multiple_violation column if the student has more than 6 or more than 7 classes do_update_stmt = insert_stmt.on_conflict_do_update( constraint="student_id_pk", set_=dict(number_of_classes_taken=student["classes_added"], one_violation=??, multiple_violation=??), ) </code>
from sqlalchemy.dialects.postgressql import insert

# Iterate the list of students to be added
for student in students:
    insert_stmt = insert(students).values(
        student_id={student['student_id']}, 
        student_name={student['name']}, 
        number_of_classes_taken={student['classes_added']},
        one_violation={False},
        multiple_violation={False})
    # If a student exists based off the primary key (the student_id) update the row with the sum(classes_added + number_of_classes_taken) and update the one_violation and multiple_violation column if the student has more than 6 or more than 7 classes
    do_update_stmt = insert_stmt.on_conflict_do_update(
        constraint="student_id_pk",
        set_=dict(number_of_classes_taken=student["classes_added"], one_violation=??,                                            multiple_violation=??),
        )

This attempt came from here: https://docs.sqlalchemy.org/en/20/dialects/postgresql.html#insert-on-conflict-upsert

I think I am close but I can’t figure it out. I also do not know how to do the sum() for classes taken or how to update the one_violation and multiple_violation off that value. Ideally, I’d like to be able to do this in one call to the DB, otherwise it’s fine to do the insert/update in one call and the updating of one_violation and multiple_violation in another.

I am also open to other approaches if there’s a better way to do this. My connection to RDS is done through SQLAlchemy create_engine and this works.

I’m pretty new to SQLAlchemy so any help is appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật