Search code examples
pythonmysqlormpeewee

Specifying Table to Connect to with Python Peewee


I have read the Peewee MySQL API documentation and this question; however, what I do not understand is how to connect to a specified table in a db using Peewee. Essentially all I'm trying to do is connect to a a table called Persons in a db called as_schema, set up some sort of basic object-relational mapping, and print out all entries' aNum column values.

My table Persons that I'm trying to read from has the following columns:

  • varchar called aNum
  • bool called access
  • bool called ajar
  • bool called ebr
  • date called weekof

My code consists of the following:

import peewee
from peewee import *

db = MySQLDatabase('as_schema', user='root',passwd='')#this connection path works perfectly, tried it using the standard MySQLdb.connect

class Person(Model):
    class Meta:
        database = db

class User(Person):
    aNum = CharField()

Person.create_table()
person = User(aNum = 'a549189')
person.save();

for person in Person:
    print person.aNum

The error I'm getting is: enter image description here


Solution

  • class Person(Model):
        class Meta:
            database = db
            db_table = 'Persons'  # Add this.
    

    In the docs, you can find a list of supported meta options:

    http://docs.peewee-orm.com/en/latest/peewee/models.html#model-options-and-table-metadata