Search code examples
pythonsqlmysqlflask

Flask website can't insert or update into certain tables on AWS EC2 MySQL server


I made a flask app that runs perfectly on a local sql and flask server, and I was able to deploy it using AWS EC2 instance, where i have a MySQL service and nginx running to make it publicly available on nostalggio.com.

The website just has users create profiles, and each user can then create game records which contain information about their accounts on a certain platform (ex. a game record has info like platform, username, dates). You can visit this sample profile to help explain the concept.

The problem is that not all Database operations are working and it's quite random, refer to the below list:

WORKING:

  • selecting DB rows and entries
  • updating user details (username,email,password,bio)
  • deleting game records completely

NOT WORKING:

  • updating a game records info (ex. changing the platform from playstation to xbox)
  • adding new game records
  • creating new users

There's too many files and snippets to put in the post but here's 1 example of working and 1 example of not working. Note that i'm using mysql.connector

#Updating user bio (working) 
        mycursor.execute("UPDATE users SET bio = %s WHERE user_id = %s",(new_bio, user_id))
        mydb.commit()  

#Inserting a new user
     mycursor.execute("INSERT INTO users (user,email,password) VALUES (%s,%s,%s)",(user,email,password))
        mydb.commit()   `

Again, this works perfectly on a local server, so my assumption is that this has to do with SQL syntax or schema differences between myphpadmin and MySQL, though i'm pretty sure that i've resolved all the differences between them. I also had no issues remotely connecting to the MySQL server.

Please do let me know of any ideas or if you'd like to me to share more code details.

I've tried to check logs for any errors but it doesn't really show me any, so i'm completely lost.


Solution

  • Issue turned out to be because of the "default values" on certain types in MySQL.

    There were some differences between the server I was running locally and the cloud server which ended up causing issues when the Flask app was trying to create or update records with optional columns, for example an optional column should have an empty string by default, but instead it was being taken as null.