Peewee 简明教程
Peewee - Primary and Composite Keys
建议关系数据库中的表应当具有一个应用了主键约束的列。相应地,Peewee 模型类也可以指定具有 primary-key 设置为 True 的字段属性。但是,如果模型类没有任何主键,Peewee 自动创建一个,名称为 “id”。请注意,上面定义的 User 模型没有任何明确定义为主键的字段。因此,我们在数据库中映射的 User 表具有一个 id 字段。
It is recommended that the table in a relational database, should have one of the columns applied with primary key constraint. Accordingly, Peewee Model class can also specify field attribute with primary-key argument set to True. However, if model class doesn’t have any primary key, Peewee automatically creates one with the name “id”. Note that the User model defined above doesn’t have any field explicitly defined as primary key. Hence, the mapped User table in our database has an id field.
要定义一个自动递增整型主键,使用 AutoField 对象作为模型中的一个属性。
To define an auto-incrementing integer primary key, use AutoField object as one attribute in the model.
class User (Model):
user_id=AutoField()
name=TextField()
age=IntegerField()
class Meta:
database=db
db_table='User'
该内容将转换为以下 CREATE TABLE 查询 −
This will translate into following CREATE TABLE query −
CREATE TABLE User (
user_id INTEGER NOT NULL
PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER NOT NULL
);
您还可以通过将 primary_key 参数设置为 True,来将任何非整型字段分配为一个主键。假设我们想将某个字母数字值作为 user_id 存储。
You can also assign any non-integer field as a primary key by setting primary_key parameter to True. Let us say we want to store certain alphanumeric value as user_id.
class User (Model):
user_id=TextField(primary_key=True)
name=TextField()
age=IntegerField()
class Meta:
database=db
db_table='User'
但是,当模型包含非整型字段作为主键时,模型实例的 save() 方法不会导致数据库驱动程序自动生成新的 ID,因此我们需要传递 force_insert=True 参数。但是,请注意 create() 方法隐式指定 force_insert 参数。
However, when model contains non-integer field as primary key, the save() method of model instance doesn’t cause database driver to generate new ID automatically, hence we need to pass force_insert=True parameter. However, note that the create() method implicitly specifies force_insert parameter.
User.create(user_id='A001',name="Rajesh", age=21)
b=User(user_id='A002',name="Amar", age=20)
b.save(force_insert=True)
save() 方法还会更新表中现有的行,在这种情况,强制插入主键是没有必要的,因为具有唯一主键的 ID 已存在。
The save() method also updates an existing row in the table, at which time, force_insert primary is not necessary, as ID with unique primary key is already existing.
Peewee 允许定义复合主键的功能。 CompositeKey 类的对象在 Meta 类中定义为主键。在以下示例中,由 User 模型的 name 和 city 字段组成的复合键已被分配为复合键。
Peewee allows feature of defining composite primary key. Object of CompositeKey class is defined as primary key in Meta class. In following example, a composite key consisting of name and city fields of User model has been assigned as composite key.
class User (Model):
name=TextField()
city=TextField()
age=IntegerField()
class Meta:
database=db
db_table='User'
primary_key=CompositeKey('name', 'city')
此模型转换为以下 CREATE TABLE 查询。
This model translates in the following CREATE TABLE query.
CREATE TABLE User (
name TEXT NOT NULL,
city TEXT NOT NULL,
age INTEGER NOT NULL,
PRIMARY KEY (
name,
city
)
);
如果您希望该表没有主键,可在模型的 Meta 类中指定 primary_key=False。
If you wish, the table should not have a primary key, then specify primary_key=False in model’s Meta class.