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

Paul Finch
Paul Finch
5,484 Points

Relational database tables with peewee

I have created a sqlite database with peewee with the following code

#models.py

from peewee import *
database = SqliteDatabase("sounds.sqlite")

class Publisher(Model):
    name = CharField(unique=True)
    class Meta:
        database = database


class Artist(Model):
    name = CharField(unique=True)
    class Meta:
        database = database


class Album(Model):
    artist = ForeignKeyField(Artist)
    title = CharField()
    release_date = DateTimeField()
    publisher = ForeignKeyField(Publisher)
    media_type = CharField()
    class Meta:
        database = database

if __name__ == "__main__":
    try:
        Artist.create_table()
    except OperationalError:
        print "Artist table already exists!"

    try:
        Album.create_table()
    except OperationalError:
        print "Album table already exists!"

    try:
        Publisher.create_table()
    except OperationalError:
        print "Publisher table already exists!"

This all works fine but then I try to add some data with the following.

#core.py
import datetime
from models import *
new_artist = Artist.create(name='The Waterboys')
new_publisher = Publisher.create(name='Virgin')
new_artist.save()
new_publisher.save()

album_one = Album(artist="The Waterboys",
                  title="This is the sea.",
                  release_date=datetime.date(1988, 12, 01),
                  publisher="Virgin",
                  media_type="CD")

album_one.save()

This gives the following error:-

Traceback (most recent call last):
  File "C:/Users/burni/PycharmProjects/PeeWeeTest/core.py", line 12, in <module>
    album_one.save()
  File "C:\Python27\lib\site-packages\peewee.py", line 4982, in save
    pk_from_cursor = self.insert(**field_dict).execute()
  File "C:\Python27\lib\site-packages\peewee.py", line 3435, in execute
    cursor = self._execute()
  File "C:\Python27\lib\site-packages\peewee.py", line 2825, in _execute
    sql, params = self.sql()
  File "C:\Python27\lib\site-packages\peewee.py", line 3403, in sql
    return self.compiler().generate_insert(self)
  File "C:\Python27\lib\site-packages\peewee.py", line 2044, in generate_insert
    return self.build_query(clauses, alias_map)
  File "C:\Python27\lib\site-packages\peewee.py", line 1831, in build_query
    return self.parse_node(Clause(*clauses), alias_map)
  File "C:\Python27\lib\site-packages\peewee.py", line 1784, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1750, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1678, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "C:\Python27\lib\site-packages\peewee.py", line 1809, in parse_node_list
    node_sql, node_params = self.parse_node(node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1784, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1750, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1678, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "C:\Python27\lib\site-packages\peewee.py", line 1809, in parse_node_list
    node_sql, node_params = self.parse_node(node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1784, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1750, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1678, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "C:\Python27\lib\site-packages\peewee.py", line 1809, in parse_node_list
    node_sql, node_params = self.parse_node(node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1784, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1750, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1665, in _parse_param
    return self.parse_node(node.adapt(node.value), alias_map, conv)
  File "C:\Python27\lib\site-packages\peewee.py", line 1475, in db_value
    return self.to_field.db_value(value)
  File "C:\Python27\lib\site-packages\peewee.py", line 958, in db_value
    return value if value is None else self.coerce(value)
ValueError: invalid literal for int() with base 10: 'The Waterboys'

Any ideas why this is happening? How do I add data to a relational database? I've tried everything.

1 Answer