Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to custom the table name in peewee?

I want to define a table which the table name is gobang_server,i write code as follow:

class BaseModel(Model):
    class Meta:
        database = database

class GobangServer(BaseModel):
    time = DateField(default=datetime.datetime.now())
    name = CharField(max_length=64)
    host = CharField(max_length=30)
    port = IntegerField()
    pid = IntegerField()

but i look at PostgreSQL the table name is "gobangserver"?
How can i define with the table name is gobang_server and the class name is not be modified.

like image 776
Sigma65535 Avatar asked Dec 29 '17 07:12

Sigma65535


People also ask

How does Peewee generate a table-name?

By default Peewee will automatically generate a table name based on the name of your model class. The way the table-name is generated depends on the value of Meta.legacy_table_names. By default, legacy_table_names=True so as to avoid breaking backwards-compatibility.

What is a field type in a peewee model?

A Model maps to the database table, a Field to the table column, and instance to the table row. Peewee uses MySQLDatabase for MySQL, PostgresqlDatabase for PostgreSQL, and SqliteDatabase for SQLite. In this tutorial, we work with SQLite database. Field types in a Peewee model define the storage type of the model.

How do I access the database in peewee?

Peewee also provides a non-ORM API to access the databases. Instead of defining models and fields, we can bind the database tables and columns to Table and Column objects defined in Peewee and execute queries with their help. To begin with, declare a Table object corresponding to the one in our database.

What is Peewee in Python?

Peewee is a simple and small Python ORM tool. It supports SQLite, MySQL and PostgreSQL. We install the peewee module. A Model maps to the database table, a Field to the table column, and instance to the table row. Peewee uses MySQLDatabase for MySQL, PostgresqlDatabase for PostgreSQL, and SqliteDatabase for SQLite.


1 Answers

class GobangServer(BaseModel):
    ...
    class Meta:
        db_table = 'gobang_server'

In peewee 3.0 it changes from "db_table" to "table_name".

like image 180
coleifer Avatar answered Sep 19 '22 13:09

coleifer