简体   繁体   English

用peewee创建一个没有rowid的表

[英]Create a table without rowid with peewee

I'm creating a small python program and i use the ORM peewee to manage my Sqlite database. 我正在创建一个小型python程序,并使用ORM peewee管理我的Sqlite数据库。 I want to create a table without primary key using peewee. 我想使用peewee创建一个没有主键的表。 The problem is that peewee is adding a primary key if i dont specify one. 问题是如果我不指定peewee会添加一个主键。 After read the docs, i found the parameter without_rowid supposed to prevent peewee to create this primary key. 阅读文档后,我发现参数no_rowid应该防止peewee创建此主键。 But it doesnt works. 但它不起作用。

Here is a small example of code : 这是一个小的代码示例:

#!/usr/bin/python
import peewee
import traceback

db_proxy = peewee.Proxy()
class BaseModel(peewee.Model):
    class Meta:
        database = db_proxy

class table1(BaseModel):
    id = peewee.IntegerField(primary_key=True)
    field1 = peewee.CharField()
    field2 = peewee.IntegerField()

class table2(BaseModel):
    table1_id = peewee.IntegerField()
    field1 = peewee.CharField()
    class Meta:
        without_rowid = True

try:
    # create database
    db = peewee.SqliteDatabase("test.db")
    db_proxy.initialize(db)
    db.create_tables([table1, table2])
except Exception as e:
    traceback.print_exc(e)

Same with the without_rowid, peewee automatically add an id primary key to my table2 table. 与without_rowid一样,peewee自动将一个id主键添加到我的table2表中。 Here is the schema of the created database : 这是创建的数据库的架构:

CREATE TABLE IF NOT EXISTS "table1"(
  "id" INTEGER NOT NULL PRIMARY KEY,
  "field1" VARCHAR(255) NOT NULL,
  "field2" INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS "table2"(
  "id" INTEGER NOT NULL PRIMARY KEY,
  "table1_id" INTEGER NOT NULL,
  "field1" VARCHAR(255) NOT NULL
) WITHOUT ROWID;

Do you know a way to solve this problem and permit me to create a table without rowid ? 您是否知道解决此问题的方法,并允许我创建不包含rowid的表?

ps : if you're asking why i want to create a table without primary key, it's because i need to insert data from a csv file. ps:如果您要问为什么我要创建没有主键的表,那是因为我需要从csv文件插入数据。 (sqlite3 database.db ".mode csv" ".import file.csv table1"). (sqlite3 database.db“ .mode csv”“ .import file.csv table1”)。 As i have an AUTO INCREMENT PRIAMRY KEY on my table, i need to trick a little bit by importing the csv file in a temporary table without primary key as explained here : http://objectiveme.com/populating-a-sqlite-database-using-csv/ 因为我的表上有一个AUTO INCREMENT PRIAMRY KEY,所以我需要通过在没有主键的临时表中导入csv文件来进行一些欺骗,如此处所述: http : //objectiveme.com/populating-a-sqlite-database -using-csv /

Thx ! 谢谢 ! :) :)

Peewee documentation is also VERY CLEAR about how to disable primary key: Peewee文档也非常清楚如何禁用主键:

http://docs.peewee-orm.com/en/latest/peewee/models.html#models-without-a-primary-key http://docs.peewee-orm.com/zh-CN/latest/peewee/models.html#models-without-a-primary-key

Please READ THE DOCS. 请阅读DOCS。

To create a Peewee model without a primary key (which is distinctly different from "WITHOUT ROWID"), you can specify "primary_key = False": 要创建没有主键的Peewee模型(与“ WITHOUT ROWID”明显不同),可以指定“ primary_key = False”:

class NoPKModel(Model):
    key = TextField()
    data = TextField()
    class Meta:
        primary_key = False

This will not automatically create an "id" field. 不会自动创建“ id”字段。

Without ROWID is a SQLite optimization with rather specific use-case. 没有ROWID是具有相当特定用例的SQLite优化。 Please read the SQLite documentation and understand the SQLite data-model before using it: https://www.sqlite.org/rowidtable.html 在使用之前,请阅读SQLite文档并了解SQLite数据模型: https : //www.sqlite.org/rowidtable.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM