The issue was with the declared table_name variable. In MySQL, TABLE_NAME is a keyword. Hence, using it led to weird behavior. Changing the variable name to table_name_ fixed the issue.
I usually rely on my IDE to highlight all keywords in a special color but for some reason, PyCharm never highlighted it anyhow, making me believe it's ok to use it...