Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python app - databases

Hi people, as I am learnin Python, I am trying to build my own app. It's a small app for banknote collectors. I already fugured out, how to make data persistent by saving everything into a text file. In the database course taught by Kenneth, the teacher showed us how to save objects into the database. I tried the same - (look into the mynotes.py file - save_entries function). When I add the notes for the first time, everything seems to be ok, when i try to save entries again, I keep getting IntegrityError saying that the serie column is not unique, but I haven't set it to be unique (see dbmodels.py). What am I doing wrong?

Any help will be appreciated..

Tomas

3 Answers

I think the problem is that you are trying to create the same record multiple times, instead of updating the existing record, therefore resulting in a database integrity error. I'm wondering what you are using for a primary key (is it somehow automatically created?). You should write some additional code to determine if a record already exists before trying to save it, if it exists then do an update instead of a create, and vice-versa.

If you are looking to make a web app version in the future, I would highly recommend switching to Flask or Django, both of which will handle some of the database trickiness for you.

Hope this helps!

Hi Jeff, thanks a lot for your reply. I will try some test before writing to the database. About the primary key, I did not create it, so it must be somehow created automatically... I think. And thanks for the recommendation, after I solve this database problem, I will definitely try Django (there is a course on Django I believe..).

Tomas

So I tried to add some additional code - look here - All the values are unique, but... when I print out the objects in the banknote list, all the objects use only 2 addresses in the memory, this is the problem I think. Any ideas how to solve this?

Tomas