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 Using Databases in Python Gettin' CRUD-y With It Add An Entry

Trey Grider
Trey Grider
699 Points

I am getting an error when I try to save an entry, would anyone be available to provide a little guidance?

Receiving this error when I try to save an entry:

Traceback (most recent call last):
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 2936, in execute_sql
    cursor.execute(sql, params or ())
sqlite3.IntegrityError: NOT NULL constraint failed: entry.context

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "diary.py", line 62, in <module>
  File "diary.py", line 34, in menu_loop
  File "diary.py", line 43, in add_entry
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 5926, in create
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 6110, in save
    pk_from_cursor = self.insert(**field_dict).execute()
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 1778, in inner
    return method(self, database, *args, **kwargs)
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 1849, in execute
    return self._execute(database)
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 2567, in _execute
    return super(Insert, self)._execute(database)
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 2316, in _execute
    cursor = database.execute(self)
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 2949, in execute
    return self.execute_sql(sql, params, commit=commit)
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 2943, in execute_sql
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 2725, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 183, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Trey Grider\Anaconda3\lib\site-packages\peewee.py", line 2936, in execute_sql
    cursor.execute(sql, params or ())
peewee.IntegrityError: NOT NULL constraint failed: entry.context
Trey Grider
Trey Grider
699 Points

My Code:

from peewee import *

from collections import OrderedDict
import datetime
import sys

db = SqliteDatabase('diary.db')

class Entry(Model):
    context = TextField() 
    timestamp = DateTimeField(default=datetime.datetime.now)

    class Meta:
        database = db

def initialize():
    """create the database and the table if they don't exist"""
    db.create_tables([Entry], safe=True)

def menu_loop():
    """show the menu"""
    choice = None 

    while choice != 'q':
        print("Enter 'q' to quit.")
        for key, value in menu.items():
            print('{}) {}'.format(key, value.__doc__))
        choice = input('Action: ').lower().strip()

        if choice in menu:

def add_entry():
    """add an entry"""
    print("Enter your entry. Press ctrl+Z when finished.")
    data = sys.stdin.read().strip()

    if data:
        if input('Save entry? [Yn] ').lower() != 'n':
            print("Saved successfully!")

def view_entries():
    """view previous entries"""

def delete_entry(entry):
    """delete an entry"""

menu = OrderedDict([
    ('a', add_entry),
    ('v', view_entries),

if __name__ == '__main__':

MOD: I just modified your question and comment to format the output and the code so it's a lot more readable to those who would like to help you out. You can find out how to do this in the Markdown Cheatsheet that's just below the textbox where you type questions or comments. Don't worry, it's easy! :-)

2 Answers

you have Entry.create(content=data), however your Entry class does not have content as a parameter (maybe you meant Entry.create(context=data)?)

I had a similar problem, and Brandon's answer is helpful because it would fix the problem.

My issue was that in my Entry Class I had misspelled the attribute variable "content" as "contetnt", you have context as an attribute in the Entry class and content=data below (this is just as Kenneth does in the video).

Even after fixing my typo in he Entry class attribute I also had to delete the diary.db, probably because it was setup with the wrong column in the table.