1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.