python to mysql insert error -


i'm trying insert data database hosted on network solutions using python , code below. able connect db when tried insert data table error:

1452 (23000): cannot add or update child row: foreign key constraint fails (`focused`.`test_results`, constraint `test_results_practice_tests_fk` foreign key (`practice_test_id1`) references `practice_tests` (`practice_test_id`))  import mysql.connector mysql.connector import errorcode  config = {   'user': '{user}',   'password': '{psswd}',   'host': '{host}',   'database': '{dbname}',   'raise_on_warnings': true, }  try:   cnx = mysql.connector.connect(**config)   cursor = cnx.cursor()    query = """insert test_results (result_id, student_id1, practice_test_id1, section_1_score, section_1_missed, section_2_score, section_2_missed, section_3_score, section_3_missed, section_4_score, section_4_missed, e_reading_score, e_analysis_score, e_writing_score, command_score, command_missed, words_score, words_missed, expression_score, expression_missed, heart_score, heart_missed, standard_score, standard_missed, problem_score, problem_missed, passport_score, passport_missed, history_score, history_missed, science_score, science_missed, math_score, reading_score) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""   data_test_result = (1, 1, 1, 1, '', 1, '', 1, '', 1, '', 1, 1, 1, 1, '', 1, '', 1, '', 1, '', 1, '', 1, '', 1, '', 1, '', 1, '', 1, 1)   cursor.execute(query, data_test_result)   except mysql.connector.error err:   if err.errno == errorcode.er_access_denied_error:     print("something wrong user name or password")   elif err.errno == errorcode.er_bad_db_error:     print("database not exist")   else:     print(err) else:   cnx.close() 

this picture of table structure reference. table structure

i wondering can solve problem , insert data table. in advance!

the meaning of error message database structure defines relationship between table inserting data , table called practice_tests. values in practise_test_id1 column therefore required exist primary key values in practice_tests table maintain "relational integrity" of structure.

you should find if insert appropriate row practice_tests code work - don't forget call connection's commit method otherwise changes won't made permanent.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -