Search code examples
pythonpython-2.7mysql-pythonpeewee

AttributeError: 'MySQLDatabase' object has no attribute 'create_table'


I am trying to connect to MySQL database and create a table:

import MySQLdb as mdb
from peewee import *

con = mdb.connect(host='127.0.0.1', user=db_user, db=db_name, passwd=db_pass)
database = MySQLDatabase(db_name, user=db_user, password=db_pass)

class BaseModel(Model):
    class Meta:
        database=database
        
class Foo(BaseModel):
    inputId = CharField(primary_key=True, index=True)
    inputContent = CharField()
    
database.connect()
database.create_table(Foo)

However, when I run the above code snippet it gives me an error:

AttributeError                            Traceback (most recent call last)
<ipython-input-93-d0c7be80ee6f> in <module>()
     14 
     15 database.connect()
---> 16 database.create_table(Foo)

AttributeError: 'MySQLDatabase' object has no attribute 'create_table'

What can be a problem? I am using Python 2.7.18, peewee (3.10.0)


Solution

  • Use create_tables():

    database.create_tables([Foo])
    

    Or:

    Foo.create_table()
    

    This is all covered in the documentation quite succinctly: http://docs.peewee-orm.com/en/latest/peewee/models.html#creating-model-tables