jgauthier
jgauthier

Reputation: 432

Peewee creating tables in sqlite instead of mysql

Pretty new to Peewee. I am modeling my code after another project that has similar functionality. But the issue I am having is that when I create tables, an sqlite DB is created and the tables in it. Although, I am trying to use MySQL.

Relevant code bits:

class MyRetryDB(RetryOperationalError, PooledMySQLDatabase):
    pass

def init_database():
    if args.db_type == 'mysql':
        log.info('Connecting to MySQL database on %s:%i...',
                 args.db_host, args.db_port)
        connections = args.db_max_connections
        db = MyRetryDB(
            args.db_name,
            user=args.db_user,
            password=args.db_pass,
            host=args.db_host,
            port=args.db_port,
            max_connections=connections,
            stale_timeout=300)
        pprint.pprint(vars(db))
        create_tables(db)
    return db

def create_tables(db):
    pprint.pprint(vars(db))
    tables = [Table1, Table2]
    db.connect()
    for table in tables:
        log.info("Creating table: %s", table.__name__)
        db.create_tables([table], safe=True)
    db.close()

Is there a something I might be missing with this?

Upvotes: 1

Views: 528

Answers (1)

coleifer
coleifer

Reputation: 26235

You need to associate your models with the database in this way:

class MyTable(Model):
    class Meta:
        database = mysql_db

Otherwise they'll use a default sqlite db.

Upvotes: 1

Related Questions