I am trying to insert records into a database I have created with MySQL. However, there is one table that I can't seem to get data into. I am getting a Lock Wait Timeout Exceeded
warning, and when I run SHOW ENGINE INNODB STATUS
this is the output I get:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2023-02-23 15:55:19 0x491c Transaction:
TRANSACTION 6740, ACTIVE 1 sec inserting, thread declared inside InnoDB 5000
mysql tables in use 1, locked 1
5 lock struct(s), heap size 1128, 2 row lock(s), undo log entries 1
MySQL thread id 57428, OS thread handle 18716, query id 363298 localhost 127.0.0.1 sqluser update
INSERT INTO quiz_submission (quiz_submission_id, submission_time, quiz_submission_attempts, quiz_submission_time_spent, quiz_submission_kept_score, quiz_id, student_id) VALUES (58434099, '2022-05-03T04:24:26Z', 1, 558917, 8.0, 3209045, 17401),(58434099, '2022-05-03T04:24:26Z', 1, 558917, 8.0, 3209045, 17401),(58434099, '2022-05-03T04:24:26Z', 1, 558917, 8.0, 3209045, 17401),(58434099, '2022-05-03T04:24:26Z', 1, 558917, 8.0, 3209045, 17401),(58434099, '2022-05-03T04:24:26Z', 1, 558917, 8.0, 3209045, 17401),(58434099, '2022-05-03T04:24:26Z', 1, 558917, 8.0, 3209045, 17401),(58434099, '2022-05-0
Foreign key constraint fails for table `canvas_student_info`.`quiz_submission`:
,
CONSTRAINT `fk_quiz_submission_student1` FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`)
Trying to add in child table, in index fk_quiz_submission_student1_idx tuple:
DATA TUPLE: 2 fields;
0: len 4; hex 800043f9; asc C ;;
1: len 4; hex 837ba233; asc { 3;;
But in parent table `canvas_student_info`.`student`, in index PRIMARY,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 4; hex 80007738; asc w8;;
1: len 6; hex 000000001947; asc G;;
2: len 7; hex 81000001070137; asc 7;;
3: len 14; hex 4d696e646920416e646572736f6e; asc Mindi Anderson;;
4: len 15; hex 416e646572736f6e2c204d696e6469; asc Anderson, Mindi;;
5: len 4; hex 8002c866; asc f;;
The odd thing is that when I check the data I am trying to insert, it matches the primary key of the other table exactly.
I will include my code as well, any help would be greatly appreciated.
# %%
import json
import mysql.connector
import requests
import config
import pandas as pd
# %%
#login to local instance server
dbUser = config.dbUser
dbPassword = config.dbPassword
dbName = config.dbName # name of the database
#login into Canvas API, current instructor Key
course = config.course #Econ151 S22
token = config.token
# %%
def insertIntoQuizSubmission(rows):
cnx = mysql.connector.connect(user=dbUser, password=dbPassword, database=dbName)
cursor = cnx.cursor()
add_student = ("INSERT INTO quiz_submission (quiz_submission_id, submission_time, quiz_submission_attempts, quiz_submission_time_spent, quiz_submission_kept_score, quiz_id, student_id) " + " VALUES (%s, %s, %s, %s, %s, %s, %s)")
# Insert World Population
cursor.executemany(add_student, rows)
# Make sure data is committed to the database
cnx.commit()
if cnx.is_connected():
cursor.close()
cnx.close()
print("closed")
# %%
def retrieveTableData(column, table):
cnx = mysql.connector.connect(user=dbUser, password=dbPassword, database=dbName)
cursor = cnx.cursor()
cursor.execute(f'SELECT {column} FROM {table}')
results = cursor.fetchall()
return results
# %%
quiz_ids = []
quiz_id_tuple = retrieveTableData('quiz_id','quiz')
for i in quiz_id_tuple:
quiz_ids.append(i[0])
# %%
student_ids = []
student_id_tuple = retrieveTableData('student_id','student')
for i in student_id_tuple:
student_ids.append(i[0])
# %%
pageNum = 1
isNextPage = True
responses = []
headers = {"Authorization": "Bearer " + token}
for student_id in student_ids:
for quiz_id in quiz_ids:
endpoint = "https://byui.instructure.com:443/api/v1/courses/" + str(course) + "/quizzes/" + str(quiz_id) + "/submissions?page=" + str(pageNum)
response = requests.get(endpoint, headers=headers)
responseJson = response.json()
responses.append(responseJson)
# %%
print(responses[0]["quiz_submissions"][0]["id"])
# %%
quiz_submissions = []
for response in responses:
submission = response["quiz_submissions"]
for quiz_submission in submission:
for student_id in student_ids:
currSubmission = (
quiz_submission["id"],
quiz_submission["finished_at"],
quiz_submission["attempt"],
quiz_submission["time_spent"],
quiz_submission["kept_score"],
quiz_submission["quiz_id"],
quiz_submission["user_id"]
)
quiz_submissions.append(currSubmission)
# %%
insertIntoQuizSubmission(quiz_submissions)
I have tried multiple steps to check the data I am entering to ensure it is the same data and it does not match.
I have also seen similar questions but they do not provide an effective answer for my particular problem.