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.
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.
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.
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.
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.
class GobangServer(BaseModel):
...
class Meta:
db_table = 'gobang_server'
In peewee 3.0 it changes from "db_table" to "table_name".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With