0

I am fairly new to sqlalchemy, however i have a json file which i am dumping to the database and i don't want to overwrite existing data in the database, i just want it to append to the database

below is my code that dumps data to the database

with open('questions.json') as f:
                    final_question_data=f.read()
                    jsondata=json.loads(final_question_data)
                for item in range(len(jsondata)): 
                    r = Question(id=int(jsondata[item]['id']), question=jsondata[item]['question'], option1=jsondata[item]['option1'],
                    option2=jsondata[item]['option2'], option3=jsondata[item]['option3'], option4=jsondata[item]['option4'],
                    correct_option=jsondata[item]['correct_option'], exam_id=jsondata[item]['exam_id'])
                    db.session.add(r)
                db.session.commit() 

1 Answer 1

1

The problem you're experiencing might be because you're specifying an "id" in your Question object. Ids are usually used to determine which row is affected in your database - if an Object uses an id which is already in the db, it will overwrite that row.

You do not need to specify the id, it should auto-increment and "append" a new row at the end of your db - if your table is setup correctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.