APIs
- class
SqliteExtDatabase
(database[, pragmas=None[, timeout=5[, c_extensions=None[, rank_functions=True[, hash_functions=False[, regexp_function=False[, bloomfilter=False]]]]]]])
Parameters:
- pragmas (list) – A list of 2-tuples containing pragma key and value toset every time a connection is opened.
- timeout – Set the busy-timeout on the SQLite driver (in seconds).
- c_extensions (bool) – Declare that C extension speedups must/must-notbe used. If set to
True
and the extension module is not available,will raise anImproperlyConfigured
exception. - rank_functions (bool) – Make search result ranking functions available.
- hash_functions (bool) – Make hashing functions available (md5, sha1, etc).
- regexp_function (bool) – Make the REGEXP function available.
- bloomfilter (bool) – Make the bloom filter available.
Extends SqliteDatabase
and inherits methods for declaringuser-defined functions, pragmas, etc.
- class
CSqliteExtDatabase
(database[, pragmas=None[, timeout=5[, c_extensions=None[, rank_functions=True[, hash_functions=False[, regexp_function=False[, bloomfilter=False[, replace_busy_handler=False]]]]]]]])
Parameters:
- pragmas (list) – A list of 2-tuples containing pragma key and value toset every time a connection is opened.
- timeout – Set the busy-timeout on the SQLite driver (in seconds).
- c_extensions (bool) – Declare that C extension speedups must/must-notbe used. If set to
True
and the extension module is not available,will raise anImproperlyConfigured
exception. - rank_functions (bool) – Make search result ranking functions available.
- hash_functions (bool) – Make hashing functions available (md5, sha1, etc).
- regexp_function (bool) – Make the REGEXP function available.
- bloomfilter (bool) – Make the bloom filter available.
- replace_busy_handler (bool) – Use a smarter busy-handler implementation.
Extends SqliteExtDatabase
and requires that theplayhouse._sqlite_ext
extension module be available.
oncommit
(_fn)- Register a callback to be executed whenever a transaction is committedon the current connection. The callback accepts no parameters and thereturn value is ignored.
However, if the callback raises a ValueError
, thetransaction will be aborted and rolled-back.
Example:
- db = CSqliteExtDatabase(':memory:')
- @db.on_commit
- def on_commit():
- logger.info('COMMITing changes')
onrollback
(_fn)- Register a callback to be executed whenever a transaction is rolledback on the current connection. The callback accepts no parameters andthe return value is ignored.
Example:
- @db.on_rollbackdef on_rollback(): logger.info('Rolling back changes')
onupdate
(_fn)Register a callback to be executed whenever the database is written to(via an UPDATE, INSERT or DELETE query). The callback shouldaccept the following parameters:
query
- the type of query, either INSERT, UPDATE or DELETE.- database name - the default database is named main.
- table name - name of table being modified.
- rowid - the rowid of the row being modified.The callback’s return value is ignored.
Example:
- db = CSqliteExtDatabase(':memory:')
- @db.on_update
- def on_update(query_type, db, table, rowid):
- # e.g. INSERT row 3 into table users.
- logger.info('%s row %s into table %s', query_type, rowid, table)
changes
()Return the number of rows modified in the currently-open transaction.
- Property which returns a boolean indicating if autocommit is enabled.By default, this value will be
True
except when inside atransaction (oratomic()
block).
Example:
- >>> db = CSqliteExtDatabase(':memory:')
- >>> db.autocommit
- True
- >>> with db.atomic():
- ... print(db.autocommit)
- ...
- False
- >>> db.autocommit
- True
Parameters:
- **destination** ([_SqliteDatabase_]($91d5d4e449d7d4b4.md#SqliteDatabase)) – Database object to serve asdestination for the backup.
- **pages** (_int_) – Number of pages per iteration. Default value of -1indicates all pages should be backed-up in a single step.
- **name** (_str_) – Name of source database (may differ if you used ATTACHDATABASE to load multiple databases). Defaults to “main”.
- **progress** – Progress callback, called with three parameters: thenumber of pages remaining, the total page count, and whether thebackup is complete.
Example:
- master = CSqliteExtDatabase('master.db')
- replica = CSqliteExtDatabase('replica.db')
- # Backup the contents of master to replica.
- master.backup(replica)
Parameters:
- **filename** – Filename to store the database backup.
- **pages** (_int_) – Number of pages per iteration. Default value of -1indicates all pages should be backed-up in a single step.
- **name** (_str_) – Name of source database (may differ if you used ATTACHDATABASE to load multiple databases). Defaults to “main”.
- **progress** – Progress callback, called with three parameters: thenumber of pages remaining, the total page count, and whether thebackup is complete.
Backup the current database to a file. The backed-up data is not adatabase dump, but an actual SQLite database file.
Example:
- db = CSqliteExtDatabase('app.db')
- def nightly_backup():
- filename = 'backup-%s.db' % (datetime.date.today())
- db.backup_to_file(filename)
Parameters:
- **table** (_str_) – Name of table containing data.
- **column** (_str_) – Name of column containing data.
- **rowid** (_int_) – ID of row to retrieve.
- **read_only** (_bool_) – Open the blob for reading only.Returns:
Blob
instance which provides efficient access tothe underlying binary data.Return type:Blob
See Blob
and ZeroBlob
for more information.
Example:
- class Image(Model):
- filename = TextField()
- data = BlobField()
- buf_size = 1024 * 1024 * 8 # Allocate 8MB for storing file.
- rowid = Image.insert({Image.filename: 'thefile.jpg',
- Image.data: ZeroBlob(buf_size)}).execute()
- # Open the blob, returning a file-like object.
- blob = db.blob_open('image', 'data', rowid)
- # Write some data to the blob.
- blob.write(image_data)
- img_size = blob.tell()
- # Read the data back out of the blob.
- blob.seek(0)
- image_data = blob.read(img_size)
- class
RowIDField
- Primary-key field that corresponds to the SQLite
rowid
field. For moreinformation, see the SQLite documentation on rowid tables..
Example:
- class Note(Model):
- rowid = RowIDField() # Will be primary key.
- content = TextField()
- timestamp = TimestampField()
- class
DocIDField
- Subclass of
RowIDField
for use on virtual tables thatspecifically use the convention ofdocid
for the primary key. As far asI know this only pertains to tables using the FTS3 and FTS4 full-textsearch extensions.
Attention
In FTS3 and FTS4, “docid” is simply an alias for “rowid”. To reduceconfusion, it’s probably best to just always use RowIDField
and never use DocIDField
.
- class NoteIndex(FTSModel):
- docid = DocIDField() # "docid" is used as an alias for "rowid".
- content = SearchField()
- class Meta:
- database = db
- class
AutoIncrementField
- SQLite, by default, may reuse primary key values after rows are deleted. Toensure that the primary key is always monotonically increasing,regardless of deletions, you should use
AutoIncrementField
.There is a small performance cost for this feature. For more information,see the SQLite docs on autoincrement.
- class
JSONField
(json_dumps=None, json_loads=None, …) - Field class suitable for storing JSON data, with special methods designedto work with the json1 extension.
SQLite 3.9.0 added JSON support inthe form of an extension library. The SQLite json1 extension provides anumber of helper functions for working with JSON data. These APIs areexposed as methods of a special field-type, JSONField
.
To access or modify specific object keys or array indexes in a JSONstructure, you can treat the JSONField
as if it were adictionary/list.
Parameters:
- json_dumps – (optional) function for serializing data to JSONstrings. If not provided, will use the stdlib
json.dumps
. - json_loads – (optional) function for de-serializing JSON to Pythonobjects. If not provided, will use the stdlib
json.loads
.
Note
To customize the JSON serialization or de-serialization, you canspecify a custom json_dumps
and json_loads
callables. Thesefunctions should accept a single paramter: the object to serialize, andthe JSON string, respectively. To modify the parameters of the stdlibJSON functions, you can use functools.partial
:
- # Do not escape unicode code-points.
- my_json_dumps = functools.partial(json.dumps, ensure_ascii=False)
- class SomeModel(Model):
- # Specify our custom serialization function.
- json_data = JSONField(json_dumps=my_json_dumps)
Let’s look at some examples of using the SQLite json1 extension withPeewee. Here we’ll prepare a database and a simple model for testing thejson1 extension:
- >>> from playhouse.sqlite_ext import *
- >>> db = SqliteExtDatabase(':memory:')
- >>> class KV(Model):
- ... key = TextField()
- ... value = JSONField()
- ... class Meta:
- ... database = db
- ...
- >>> KV.create_table()
Storing data works as you might expect. There’s no need to serializedictionaries or lists as JSON, as this is done automatically by Peewee:
- >>> KV.create(key='a', value={'k1': 'v1'})
- <KV: 1>
- >>> KV.get(KV.key == 'a').value
- {'k1': 'v1'}
We can access specific parts of the JSON data using dictionary lookups:
- >>> KV.get(KV.value['k1'] == 'v1').key
- 'a'
It’s possible to update a JSON value in-place using the update()
method. Note that “k1=v1” is preserved:
- >>> KV.update(value=KV.value.update({'k2': 'v2', 'k3': 'v3'})).execute()
- 1
- >>> KV.get(KV.key == 'a').value
- {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}
We can also update existing data atomically, or remove keys by settingtheir value to None
. In the following example, we’ll update the valueof “k1” and remove “k3” (“k2” will not be modified):
- >>> KV.update(value=KV.value.update({'k1': 'v1-x', 'k3': None})).execute()
- 1
- >>> KV.get(KV.key == 'a').value
- {'k1': 'v1-x', 'k2': 'v2'}
We can also set individual parts of the JSON data using the set()
method:
- >>> KV.update(value=KV.value['k1'].set('v1')).execute()
- 1
- >>> KV.get(KV.key == 'a').value
- {'k1': 'v1', 'k2': 'v2'}
The set()
method can also be used with objects, inaddition to scalar values:
- >>> KV.update(value=KV.value['k2'].set({'x2': 'y2'})).execute()
- 1
- >>> KV.get(KV.key == 'a').value
- {'k1': 'v1', 'k2': {'x2': 'y2'}}
Individual parts of the JSON data can be removed atomically as well, usingremove()
:
- >>> KV.update(value=KV.value['k2'].remove()).execute()
- 1
- >>> KV.get(KV.key == 'a').value
- {'k1': 'v1'}
We can also get the type of value stored at a specific location in the JSONdata using the json_type()
method:
- >>> KV.select(KV.value.json_type(), KV.value['k1'].json_type()).tuples()[:]
- [('object', 'text')]
Let’s add a nested value and then see how to iterate through it’s contentsrecursively using the tree()
method:
- >>> KV.create(key='b', value={'x1': {'y1': 'z1', 'y2': 'z2'}, 'x2': [1, 2]})
- <KV: 2>
- >>> tree = KV.value.tree().alias('tree')
- >>> query = KV.select(KV.key, tree.c.fullkey, tree.c.value).from_(KV, tree)
- >>> query.tuples()[:]
- [('a', '$', {'k1': 'v1'}),
- ('a', '$.k1', 'v1'),
- ('b', '$', {'x1': {'y1': 'z1', 'y2': 'z2'}, 'x2': [1, 2]}),
- ('b', '$.x2', [1, 2]),
- ('b', '$.x2[0]', 1),
- ('b', '$.x2[1]', 2),
- ('b', '$.x1', {'y1': 'z1', 'y2': 'z2'}),
- ('b', '$.x1.y1', 'z1'),
- ('b', '$.x1.y2', 'z2')]
The tree()
and children()
methodsare powerful. For more information on how to utilize them, see thejson1 extension documentation.
Also note, that JSONField
lookups can be chained:
- >>> query = KV.select().where(KV.value['x1']['y1'] == 'z1')
- >>> for obj in query:
- ... print(obj.key, obj.value)
- ...
- 'b', {'x1': {'y1': 'z1', 'y2': 'z2'}, 'x2': [1, 2]}
For more information, refer to the sqlite json1 documentation.
Parameters:item – Access a specific key or array index in the JSON data.Returns:a special object exposing access to the JSON data.Return type:JSONPath
Access a specific key or array index in the JSON data. Returns aJSONPath
object, which exposes convenient methods forreading or modifying a particular part of a JSON object.
Example:
- # If metadata contains {"tags": ["list", "of", "tags"]}, we can
- # extract the first tag in this way:
- Post.select(Post, Post.metadata['tags'][0].alias('first_tag'))
For more examples see the JSONPath
API documentation.
Parameters:
- **value** – a scalar value, list, or dictionary.
- **as_json** (_bool_) – force the value to be treated as JSON, in whichcase it will be serialized as JSON in Python beforehand. Bydefault, lists and dictionaries are treated as JSON to beserialized, while strings and integers are passed as-is.
Set the value stored in a JSONField
.
Uses the json_set() functionfrom the json1 extension.
Parameters:data – a scalar value, list or dictionary to merge with the datacurrently stored in a JSONField
. To remove a particularkey, set that key to None
in the updated data.
Merge new data into the JSON value using the RFC-7396 MergePatchalgorithm to apply a patch (data
parameter) against the columndata. MergePatch can add, modify, or delete elements of a JSON object,which means update()
is a generalized replacementfor both set()
and remove()
.MergePatch treats JSON array objects as atomic, so update()
cannotappend to an array, nor modify individual elements of an array.
For more information as well as examples, see the SQLite json_patch()function documentation.
remove
()- Remove the data stored in the
JSONField
.
Uses the json_remove functionfrom the json1 extension.
The type returned will be one of:
- object
- array
- integer
- real
- true
- false
- text
- null <– the string “null” means an actual NULL value
- NULL <– an actual NULL value means the path was not found
Uses the json_typefunction from the json1 extension.
Uses the json_array_lengthfunction from the json1 extension.
children
()- The
children
function corresponds tojson_each
, a table-valuedfunction that walks the JSON value provided and returns the immediatechildren of the top-level array or object. If a path is specified, thenthat path is treated as the top-most element.
The rows returned by calls to children()
have the followingattributes:
- <code>key</code>: the key of the current element relative to its parent.
- <code>value</code>: the value of the current element.
- <code>type</code>: one of the data-types (see [<code>json_type()</code>](#JSONField.json_type)).
- <code>atom</code>: the scalar value for primitive types, <code>NULL</code> for arrays and objects.
- <code>id</code>: a unique ID referencing the current node in the tree.
- <code>parent</code>: the ID of the containing node.
- <code>fullkey</code>: the full path describing the current element.
- <code>path</code>: the path to the container of the current row.
Internally this method uses the json_each(documentation link) function from the json1 extension.
Example usage (compare to tree()
method):
- class KeyData(Model):
- key = TextField()
- data = JSONField()
- KeyData.create(key='a', data={'k1': 'v1', 'x1': {'y1': 'z1'}})
- KeyData.create(key='b', data={'x1': {'y1': 'z1', 'y2': 'z2'}})
- # We will query the KeyData model for the key and all the
- # top-level keys and values in it's data field.
- kd = KeyData.data.children().alias('children')
- query = (KeyData
- .select(kd.c.key, kd.c.value, kd.c.fullkey)
- .from_(KeyData, kd)
- .order_by(kd.c.key)
- .tuples())
- print(query[:])
- # PRINTS:
- [('a', 'k1', 'v1', '$.k1'),
- ('a', 'x1', '{"y1":"z1"}', '$.x1'),
- ('b', 'x1', '{"y1":"z1","y2":"z2"}', '$.x1')]
tree
()- The
tree
function corresponds tojson_tree
, a table-valuedfunction that recursively walks the JSON value provided and returnsinformation about the keys at each level. If a path is specified, thenthat path is treated as the top-most element.
The rows returned by calls to tree()
have the same attributes asrows returned by calls to children()
:
- <code>key</code>: the key of the current element relative to its parent.
- <code>value</code>: the value of the current element.
- <code>type</code>: one of the data-types (see [<code>json_type()</code>](#JSONField.json_type)).
- <code>atom</code>: the scalar value for primitive types, <code>NULL</code> for arrays and objects.
- <code>id</code>: a unique ID referencing the current node in the tree.
- <code>parent</code>: the ID of the containing node.
- <code>fullkey</code>: the full path describing the current element.
- <code>path</code>: the path to the container of the current row.
Internally this method uses the json_tree(documentation link) function from the json1 extension.
Example usage:
- class KeyData(Model):
- key = TextField()
- data = JSONField()
- KeyData.create(key='a', data={'k1': 'v1', 'x1': {'y1': 'z1'}})
- KeyData.create(key='b', data={'x1': {'y1': 'z1', 'y2': 'z2'}})
- # We will query the KeyData model for the key and all the
- # keys and values in it's data field, recursively.
- kd = KeyData.data.tree().alias('tree')
- query = (KeyData
- .select(kd.c.key, kd.c.value, kd.c.fullkey)
- .from_(KeyData, kd)
- .order_by(kd.c.key)
- .tuples())
- print(query[:])
- # PRINTS:
- [('a', None, '{"k1":"v1","x1":{"y1":"z1"}}', '$'),
- ('b', None, '{"x1":{"y1":"z1","y2":"z2"}}', '$'),
- ('a', 'k1', 'v1', '$.k1'),
- ('a', 'x1', '{"y1":"z1"}', '$.x1'),
- ('b', 'x1', '{"y1":"z1","y2":"z2"}', '$.x1'),
- ('a', 'y1', 'z1', '$.x1.y1'),
- ('b', 'y1', 'z1', '$.x1.y1'),
- ('b', 'y2', 'z2', '$.x1.y2')]
Parameters:
- field (JSONField) – the field object we intend to access.
- path (tuple) – Components comprising the JSON path.
A convenient, Pythonic way of representing JSON paths for use withJSONField
.
The JSONPath
object implements getitem
, accumulating pathcomponents, which it can turn into the corresponding json-path expression.
Parameters:item – Access a sub-key key or array index.Returns:a JSONPath
representing the new path.
Access a sub-key or array index in the JSON data. Returns aJSONPath
object, which exposes convenient methods forreading or modifying a particular part of a JSON object.
Example:
- # If metadata contains {"tags": ["list", "of", "tags"]}, we can
- # extract the first tag in this way:
- first_tag = Post.metadata['tags'][0]
- query = (Post
- .select(Post, first_tag.alias('first_tag'))
- .order_by(first_tag))
Parameters:
- **value** – a scalar value, list, or dictionary.
- **as_json** (_bool_) – force the value to be treated as JSON, in whichcase it will be serialized as JSON in Python beforehand. Bydefault, lists and dictionaries are treated as JSON to beserialized, while strings and integers are passed as-is.
Set the value at the given location in the JSON data.
Uses the json_set() functionfrom the json1 extension.
Parameters:data – a scalar value, list or dictionary to merge with the dataat the given location in the JSON data. To remove a particular key,set that key to None
in the updated data.
Merge new data into the JSON value using the RFC-7396 MergePatchalgorithm to apply a patch (data
parameter) against the columndata. MergePatch can add, modify, or delete elements of a JSON object,which means update()
is a generalized replacementfor both set()
and remove()
.MergePatch treats JSON array objects as atomic, so update()
cannotappend to an array, nor modify individual elements of an array.
For more information as well as examples, see the SQLite json_patch()function documentation.
Uses the json_type functionfrom the json1 extension.
json_type
()- Return a string identifying the type of value stored at the givenlocation in the JSON data.
The type returned will be one of:
- object
- array
- integer
- real
- true
- false
- text
- null <– the string “null” means an actual NULL value
- NULL <– an actual NULL value means the path was not found
Uses the json_typefunction from the json1 extension.
Uses the json_array_lengthfunction from the json1 extension.
children
()Table-valued function that exposes the direct descendants of a JSONobject at the given location. See also
JSONField.children()
.- Table-valued function that exposes all descendants, recursively, of aJSON object at the given location. See also
JSONField.tree()
.
- class
SearchField
([unindexed=False[, column_name=None]]) - Field-class to be used for columns on models representing full-text searchvirtual tables. The full-text search extensions prohibit the specificationof any typing or constraints on columns. This behavior is enforced by the
SearchField
, which raises an exception if any configuration isattempted that would be incompatible with the full-text search extensions.
Example model for document search index (timestamp is stored in the tablebut it’s data is not searchable):
- class DocumentIndex(FTSModel):
- title = SearchField()
- content = SearchField()
- tags = SearchField()
- timestamp = SearchField(unindexed=True)
Parameters:term (str) – full-text search query/termsReturns:a Expression
corresponding to the MATCH
operator.
Sqlite’s full-text search supports searching either the full table,including all indexed columns, or searching individual columns. Thematch()
method can be used to restrict search toa single column:
- class SearchIndex(FTSModel):
- title = SearchField()
- body = SearchField()
- # Search *only* the title field and return results ordered by
- # relevance, using bm25.
- query = (SearchIndex
- .select(SearchIndex, SearchIndex.bm25().alias('score'))
- .where(SearchIndex.title.match('python'))
- .order_by(SearchIndex.bm25()))
To instead search all indexed columns, use theFTSModel.match()
method:
- # Searches *both* the title and body and return results ordered by
- # relevance, using bm25.
- query = (SearchIndex
- .select(SearchIndex, SearchIndex.bm25().alias('score'))
- .where(SearchIndex.match('python'))
- .order_by(SearchIndex.bm25()))
- class
VirtualModel
- Model class designed to be used to represent virtual tables. The defaultmetadata settings are slightly different, to match those frequently used byvirtual tables.
Metadata options:
arguments
- arguments passed to the virtual table constructor.extension_module
- name of extension to use for virtual table.options
- a dictionary of settings to apply in virtual table- constructor.
primary_key
- defaults toFalse
, indicating no primary key.These all are combined in the following way:
- CREATE VIRTUAL TABLE <table_name>
- USING <extension_module>
- ([prefix_arguments, ...] fields, ... [arguments, ...], [options...])
- class
FTSModel
- Subclass of
VirtualModel
to be used with the FTS3 and FTS4full-text search extensions.
FTSModel subclasses should be defined normally, however there are a couplecaveats:
- Unique constraints, not null constraints, check constraints and foreignkeys are not supported.
- Indexes on fields and multi-column indexes are ignored completely
- Sqlite will treat all column types as
TEXT
(although youcan store other data types, Sqlite will treat them as text). - FTS models contain a
rowid
field which is automatically created andmanaged by SQLite (unless you choose to explicitly set it during modelcreation). Lookups on this column are fast and efficient.Given these constraints, it is strongly recommended that all fieldsdeclared on anFTSModel
subclass be instances ofSearchField
(though an exception is made for explicitlydeclaring aRowIDField
). UsingSearchField
willhelp prevent you accidentally creating invalid column constraints. If youwish to store metadata in the index but would not like it to be included inthe full-text index, then specifyunindexed=True
when instantiating theSearchField
.
The only exception to the above is for the rowid
primary key, which canbe declared using RowIDField
. Lookups on the rowid
are veryefficient. If you are using FTS4 you can also use DocIDField
,which is an alias for the rowid (though there is no benefit to doing so).
Because of the lack of secondary indexes, it usually makes sense to usethe rowid
primary key as a pointer to a row in a regular table. Forexample:
- class Document(Model):
- # Canonical source of data, stored in a regular table.
- author = ForeignKeyField(User, backref='documents')
- title = TextField(null=False, unique=True)
- content = TextField(null=False)
- timestamp = DateTimeField()
- class Meta:
- database = db
- class DocumentIndex(FTSModel):
- # Full-text search index.
- rowid = RowIDField()
- title = SearchField()
- content = SearchField()
- class Meta:
- database = db
- # Use the porter stemming algorithm to tokenize content.
- options = {'tokenize': 'porter'}
To store a document in the document index, we will INSERT
a row intothe DocumentIndex
table, manually setting the rowid
so that itmatches the primary-key of the corresponding Document
:
- def store_document(document):
- DocumentIndex.insert({
- DocumentIndex.rowid: document.id,
- DocumentIndex.title: document.title,
- DocumentIndex.content: document.content}).execute()
To perform a search and return ranked results, we can query theDocument
table and join on the DocumentIndex
. This join will beefficient because lookups on an FTSModel’s rowid
field are fast:
- def search(phrase):
- # Query the search index and join the corresponding Document
- # object on each search result.
- return (Document
- .select()
- .join(
- DocumentIndex,
- on=(Document.id == DocumentIndex.rowid))
- .where(DocumentIndex.match(phrase))
- .order_by(DocumentIndex.bm25()))
Warning
All SQL queries on FTSModel
classes will be full-table scansexcept full-text searches and rowid
lookups.
If the primary source of the content you are indexing exists in a separatetable, you can save some disk space by instructing SQLite to not store anadditional copy of the search index content. SQLite will still create themetadata and data-structures needed to perform searches on the content, butthe content itself will not be stored in the search index.
To accomplish this, you can specify a table or column using the content
option. The FTS4 documentationhas more information.
Here is a short example illustrating how to implement this with peewee:
- class Blog(Model):
- title = TextField()
- pub_date = DateTimeField(default=datetime.datetime.now)
- content = TextField() # We want to search this.
- class Meta:
- database = db
- class BlogIndex(FTSModel):
- content = SearchField()
- class Meta:
- database = db
- options = {'content': Blog.content} # <-- specify data source.
- db.create_tables([Blog, BlogIndex])
- # Now, we can manage content in the BlogIndex. To populate the
- # search index:
- BlogIndex.rebuild()
- # Optimize the index.
- BlogIndex.optimize()
The content
option accepts either a single Field
or aModel
and can reduce the amount of storage used by the databasefile. However, content will need to be manually moved to/from theassociated FTSModel
.
Parameters:term – Search term or expression.
Generate a SQL expression representing a search for the given term orexpression in the table. SQLite uses the MATCH
operator to indicatea full-text search.
Example:
- # Search index for "search phrase" and return results ranked
- # by relevancy using the BM25 algorithm.
- query = (DocumentIndex
- .select()
- .where(DocumentIndex.match('search phrase'))
- .order_by(DocumentIndex.bm25()))
- for result in query:
- print('Result: %s' % result.title)
- classmethod
search
(term[, weights=None[, with_score=False[, score_alias='score'[, explicit_ordering=False]]]])
Parameters:
- **term** (_str_) – Search term to use.
- **weights** – A list of weights for the columns, ordered with respectto the column’s position in the table. **Or**, a dictionary keyed bythe field or field name and mapped to a value.
- **with_score** – Whether the score should be returned as part ofthe <code>SELECT</code> statement.
- **score_alias** (_str_) – Alias to use for the calculated rank score.This is the attribute you will use to access the scoreif <code>with_score=True</code>.
- **explicit_ordering** (_bool_) – Order using full SQL function tocalculate rank, as opposed to simply referencing the score aliasin the ORDER BY clause.
Shorthand way of searching for a term and sorting results by thequality of the match.
Note
This method uses a simplified algorithm for determining therelevance rank of results. For more sophisticated result ranking,use the search_bm25()
method.
- # Simple search.
- docs = DocumentIndex.search('search term')
- for result in docs:
- print(result.title)
- # More complete example.
- docs = DocumentIndex.search(
- 'search term',
- weights={'title': 2.0, 'content': 1.0},
- with_score=True,
- score_alias='search_score')
- for result in docs:
- print(result.title, result.search_score)
- classmethod
searchbm25
(_term[, weights=None[, with_score=False[, score_alias='score'[, explicit_ordering=False]]]])
Parameters:
- **term** (_str_) – Search term to use.
- **weights** – A list of weights for the columns, ordered with respectto the column’s position in the table. **Or**, a dictionary keyed bythe field or field name and mapped to a value.
- **with_score** – Whether the score should be returned as part ofthe <code>SELECT</code> statement.
- **score_alias** (_str_) – Alias to use for the calculated rank score.This is the attribute you will use to access the scoreif <code>with_score=True</code>.
- **explicit_ordering** (_bool_) – Order using full SQL function tocalculate rank, as opposed to simply referencing the score aliasin the ORDER BY clause.
Shorthand way of searching for a term and sorting results by thequality of the match using the BM25 algorithm.
Attention
The BM25 ranking algorithm is only available for FTS4. If you areusing FTS3, use the search()
method instead.
- classmethod
searchbm25f
(_term[, weights=None[, with_score=False[, score_alias='score'[, explicit_ordering=False]]]]) Same as
FTSModel.search_bm25()
, but using the BM25f variantof the BM25 ranking algorithm.classmethod
searchlucene
(_term[, weights=None[, with_score=False[, score_alias='score'[, explicit_ordering=False]]]])Same as
FTSModel.search_bm25()
, but using the result rankingalgorithm from the Lucene search engine.
Parameters:col_weight (float) – (Optional) weight to give to the ith columnof the model. By default all columns have a weight of 1.0
.
Generate an expression that will calculate and return the quality ofthe search match. This rank
can be used to sort the search results.A higher rank score indicates a better match.
The rank
function accepts optional parameters that allow you tospecify weights for the various columns. If no weights are specified,all columns are considered of equal importance.
Note
The algorithm used by rank()
is simple andrelatively quick. For more sophisticated result ranking, use:
- [<code>bm25()</code>](#FTSModel.bm25)
- [<code>bm25f()</code>](#FTSModel.bm25f)
- [<code>lucene()</code>](#FTSModel.lucene)
- query = (DocumentIndex
- .select(
- DocumentIndex,
- DocumentIndex.rank().alias('score'))
- .where(DocumentIndex.match('search phrase'))
- .order_by(DocumentIndex.rank()))
- for search_result in query:
- print search_result.title, search_result.score
Parameters:col_weight (float) – (Optional) weight to give to the ith columnof the model. By default all columns have a weight of 1.0
.
Generate an expression that will calculate and return the quality ofthe search match using the BM25 algorithm.This value can be used to sort the search results, with higher scorescorresponding to better matches.
Like rank()
, bm25
function accepts optionalparameters that allow you to specify weights for the various columns.If no weights are specified, all columns are considered of equalimportance.
Attention
The BM25 result ranking algorithm requires FTS4. If you are usingFTS3, use rank()
instead.
- query = (DocumentIndex
- .select(
- DocumentIndex,
- DocumentIndex.bm25().alias('score'))
- .where(DocumentIndex.match('search phrase'))
- .order_by(DocumentIndex.bm25()))
- for search_result in query:
- print(search_result.title, search_result.score)
Note
The above code example is equivalent to calling thesearch_bm25()
method:
- query = DocumentIndex.search_bm25('search phrase', with_score=True)
- for search_result in query:
- print(search_result.title, search_result.score)
- classmethod
bm25f
([col1_weight, col2_weight…coln_weight]) Identical to
bm25()
, except that it uses the BM25fvariant of the BM25 ranking algorithm.Identical to
bm25()
, except that it uses the Lucenesearch result ranking algorithm.Rebuild the search index – this only works when the
content
optionwas specified during table creation.- Optimize the search index.
- class
FTS5Model
- Subclass of
VirtualModel
to be used with the FTS5full-text search extensions.
FTS5Model subclasses should be defined normally, however there are a couplecaveats:
- FTS5 explicitly disallows specification of any constraints, data-type orindexes on columns. For that reason, all columns must be instancesof
SearchField
. - FTS5 models contain a
rowid
field which is automatically created andmanaged by SQLite (unless you choose to explicitly set it during modelcreation). Lookups on this column are fast and efficient. Indexes on fields and multi-column indexes are not supported.The
FTS5
extension comes with a built-in implementation of the BM25ranking function. Therefore, thesearch
andsearch_bm25
methodshave been overridden to use the builtin ranking functions rather thanuser-defined functions.Return a boolean indicating whether the FTS5 extension is installed. Ifit is not installed, an attempt will be made to load the extension.
classmethod
search
(term[, weights=None[, with_score=False[, score_alias='score']]])
Parameters:
- **term** (_str_) – Search term to use.
- **weights** – A list of weights for the columns, ordered with respectto the column’s position in the table. **Or**, a dictionary keyed bythe field or field name and mapped to a value.
- **with_score** – Whether the score should be returned as part ofthe <code>SELECT</code> statement.
- **score_alias** (_str_) – Alias to use for the calculated rank score.This is the attribute you will use to access the scoreif <code>with_score=True</code>.
- **explicit_ordering** (_bool_) – Order using full SQL function tocalculate rank, as opposed to simply referencing the score aliasin the ORDER BY clause.
Shorthand way of searching for a term and sorting results by thequality of the match. The FTS5
extension provides a built-inimplementation of the BM25 algorithm, which is used to rank the resultsby relevance.
Higher scores correspond to better matches.
- # Simple search.
- docs = DocumentIndex.search('search term')
- for result in docs:
- print(result.title)
- # More complete example.
- docs = DocumentIndex.search(
- 'search term',
- weights={'title': 2.0, 'content': 1.0},
- with_score=True,
- score_alias='search_score')
- for result in docs:
- print(result.title, result.search_score)
- classmethod
searchbm25
(_term[, weights=None[, with_score=False[, score_alias='score']]]) With FTS5,
search_bm25()
is identical to thesearch()
method.
Parameters:col_weight (float) – (Optional) weight to give to the ith columnof the model. By default all columns have a weight of 1.0
.
Generate an expression that will calculate and return the quality ofthe search match using the BM25 algorithm.This value can be used to sort the search results, with higher scorescorresponding to better matches.
The rank()
function accepts optional parametersthat allow you to specify weights for the various columns. If noweights are specified, all columns are considered of equal importance.
- query = (DocumentIndex
- .select(
- DocumentIndex,
- DocumentIndex.rank().alias('score'))
- .where(DocumentIndex.match('search phrase'))
- .order_by(DocumentIndex.rank()))
- for search_result in query:
- print(search_result.title, search_result.score)
Note
The above code example is equivalent to calling thesearch()
method:
- query = DocumentIndex.search('search phrase', with_score=True)
- for search_result in query:
- print(search_result.title, search_result.score)
- classmethod
bm25
([col1_weight, col2_weight…coln_weight]) Because FTS5 provides built-in support for BM25, the
bm25()
method is identical to therank()
method.classmethod
VocabModel
([table_type='row'|'col'|'instance'[, table_name=None]])
Parameters:
- **table_type** (_str_) – Either ‘row’, ‘col’ or ‘instance’.
- **table_name** – Name for the vocab table. If not specified, will be“fts5tablename_v”.
Generate a model class suitable for accessing the vocab tablecorresponding to FTS5 search index.
- class
TableFunction
- Implement a user-defined table-valued function. Unlike a simplescalar or aggregate function, which returnsa single scalar value, a table-valued function can return any number ofrows of tabular data.
Simple example:
- from playhouse.sqlite_ext import TableFunction
- class Series(TableFunction):
- # Name of columns in each row of generated data.
- columns = ['value']
- # Name of parameters the function may be called with.
- params = ['start', 'stop', 'step']
- def initialize(self, start=0, stop=None, step=1):
- """
- Table-functions declare an initialize() method, which is
- called with whatever arguments the user has called the
- function with.
- """
- self.start = self.current = start
- self.stop = stop or float('Inf')
- self.step = step
- def iterate(self, idx):
- """
- Iterate is called repeatedly by the SQLite database engine
- until the required number of rows has been read **or** the
- function raises a `StopIteration` signalling no more rows
- are available.
- """
- if self.current > self.stop:
- raise StopIteration
- ret, self.current = self.current, self.current + self.step
- return (ret,)
- # Register the table-function with our database, which ensures it
- # is declared whenever a connection is opened.
- db.table_function('series')(Series)
- # Usage:
- cursor = db.execute_sql('SELECT * FROM series(?, ?, ?)', (0, 5, 2))
- for value, in cursor:
- print(value)
Note
A TableFunction
must be registered with a databaseconnection before it can be used. To ensure the table function isalways available, you can use theSqliteDatabase.table_function()
decorator to register thefunction with the database.
TableFunction
implementations must provide two attributes andimplement two methods, described below.
columns
A list containing the names of the columns for the data returned by thefunction. For example, a function that is used to split a string on adelimiter might specify 3 columns:
[substring, start_idx, end_idx]
.The names of the parameters the function may be called with. Allparameters, including optional parameters, should be listed. Forexample, a function that is used to split a string on a delimiter mightspecify 2 params:
[string, delimiter]
.Optional - specify the name for the table function. If not provided,name will be taken from the class name.
print_tracebacks = True
Print a full traceback for any errors that occur in thetable-function’s callback methods. When set to False, only the genericOperationalError will be visible.
Parameters:parameter_values – Parameters the function was called with.Returns:No return value.
The initialize
method is called to initialize the table functionwith the parameters the user specified when calling the function.
Parameters:idx (int) – current iteration stepReturns:A tuple of row data corresponding to the columns namedin the columns
attribute.Raises:StopIteration – To signal that no more rows are available.
This function is called repeatedly and returns successive rows of data.The function may terminate before all rows are consumed (especially ifthe user specified a LIMIT
on the results). Alternatively, thefunction can signal that no more data is available by raising aStopIteration
exception.
Parameters:conn – A sqlite3.Connection
object.
Register the table function with a DB-API 2.0 sqlite3.Connection
object. Table-valued functions must be registered before they canbe used in a query.
Example:
- class MyTableFunction(TableFunction):
- name = 'my_func'
- # ... other attributes and methods ...
- db = SqliteDatabase(':memory:')
- db.connect()
- MyTableFunction.register(db.connection())
To ensure the TableFunction
is registered every time aconnection is opened, use the table_function()
decorator.
Parameters:
- model_class – The model class containing the nodes in the tree.
- foreign_key – The self-referential parent-node field on the modelclass. If not provided, peewee will introspect the model to find asuitable key.
- referencing_class – Intermediate table for a many-to-many relationship.
- referencing_key – For a many-to-many relationship, the originatingside of the relation.Returns:Returns a
VirtualModel
for working with a closure table.
Factory function for creating a model class suitable for working with atransitive closuretable. Closure tables are VirtualModel
subclasses that workwith the transitive closure SQLite extension. These special tables aredesigned to make it easy to efficiently query hierarchical data. The SQLiteextension manages an AVL tree behind-the-scenes, transparently updating thetree when your table changes and making it easy to perform common querieson hierarchical data.
To use the closure table extension in your project, you need:
- A copy of the SQLite extension. The source code can be found inthe SQLite code repositoryor by cloning this gist:
- $ git clone https://gist.github.com/coleifer/7f3593c5c2a645913b92 closure
- $ cd closure/
- Compile the extension as a shared library, e.g.
- $ gcc -g -fPIC -shared closure.c -o closure.so
- Create a model for your hierarchical data. The only requirement here isthat the model has an integer primary key and a self-referential foreignkey. Any additional fields are fine.
- class Category(Model):
- name = CharField()
- metadata = TextField()
- parent = ForeignKeyField('self', index=True, null=True) # Required.
- # Generate a model for the closure virtual table.
- CategoryClosure = ClosureTable(Category)
The self-referentiality can also be achieved via an intermediate table(for a many-to-many relation).
- class User(Model):
- name = CharField()
- class UserRelations(Model):
- user = ForeignKeyField(User)
- knows = ForeignKeyField(User, backref='_known_by')
- class Meta:
- primary_key = CompositeKey('user', 'knows') # Alternatively, a unique index on both columns.
- # Generate a model for the closure virtual table, specifying the UserRelations as the referencing table
- UserClosure = ClosureTable(
- User,
- referencing_class=UserRelations,
- foreign_key=UserRelations.knows,
- referencing_key=UserRelations.user)
- In your application code, make sure you load the extension when youinstantiate your
Database
object. This is done by passingthe path to the shared library to theload_extension()
method.
- db = SqliteExtDatabase('my_database.db')
- db.load_extension('/path/to/closure')
Warning
There are two caveats you should be aware of when using thetransitiveclosure
extension. First, it requires that your _sourcemodel have an integer primary key. Second, it is strongly recommendedthat you create an index on the self-referential foreign key.
Example:
- class Category(Model):
- name = CharField()
- metadata = TextField()
- parent = ForeignKeyField('self', index=True, null=True) # Required.
- # Generate a model for the closure virtual table.
- CategoryClosure = ClosureTable(Category)
- # Create the tables if they do not exist.
- db.create_tables([Category, CategoryClosure], True)
It is now possible to perform interesting queries using the data from theclosure table:
- # Get all ancestors for a particular node.
- laptops = Category.get(Category.name == 'Laptops')
- for parent in Closure.ancestors(laptops):
- print parent.name
- # Computer Hardware
- # Computers
- # Electronics
- # All products
- # Get all descendants for a particular node.
- hardware = Category.get(Category.name == 'Computer Hardware')
- for node in Closure.descendants(hardware):
- print node.name
- # Laptops
- # Desktops
- # Hard-drives
- # Monitors
- # LCD Monitors
- # LED Monitors
API of the VirtualModel
returned by ClosureTable()
.
- class
BaseClosureTable
- node = Category.get(Category.name == 'Electronics')
- # Direct child categories.
- children = CategoryClosure.descendants(node, depth=1)
- # Grand-child categories.
- children = CategoryClosure.descendants(node, depth=2)
- # Descendants at all depths.
- all_descendants = CategoryClosure.descendants(node)
- <code>ancestors</code>(_node_[, _depth=None_[, _include_node=False_]])[](#BaseClosureTable.ancestors)
-
Retrieve all ancestors of the given node. If a depth is specified,only nodes at that depth (relative to the given node) will bereturned.
- node = Category.get(Category.name == 'Laptops')
- # All ancestors.
- all_ancestors = CategoryClosure.ancestors(node)
- # Grand-parent category.
- grandparent = CategoryClosure.ancestores(node, depth=2)
- <code>siblings</code>(_node_[, _include_node=False_])[](#BaseClosureTable.siblings)
-
Retrieve all nodes that are children of the specified node’sparent.
Note
For an in-depth discussion of the SQLite transitive closure extension,check out this blog post, Querying Tree Structures in SQLite using Python and the Transitive Closure Extension.
- class
LSMTable
VirtualModel
subclass suitable for working with the lsm1 extensionThe lsm1 extension is a virtual table that provides a SQL interface tothe lsm key/value storage engine from SQLite4.
Note
The LSM1 extension has not been released yet (SQLite version 3.22 attime of writing), so consider this feature experimental with potentialto change in subsequent releases.
LSM tables define one primary key column and an arbitrary number ofadditional value columns (which are serialized and stored in a single valuefield in the storage engine). The primary key must be all of the same typeand use one of the following field types:
IntegerField
TextField
BlobField
Since the LSM storage engine is a key/value store, primary keys (includingintegers) must be specified by the application.
Attention
Secondary indexes are not supported by the LSM engine, so the onlyefficient queries will be lookups (or range queries) on the primarykey. Other fields can be queried and filtered on, but may result in afull table-scan.
Example model declaration:
- db = SqliteExtDatabase('my_app.db')
- db.load_extension('lsm.so') # Load shared library.
- class EventLog(LSMTable):
- timestamp = IntegerField(primary_key=True)
- action = TextField()
- sender = TextField()
- target = TextField()
- class Meta:
- database = db
- filename = 'eventlog.ldb' # LSM data is stored in separate db.
- # Declare virtual table.
- EventLog.create_table()
Example queries:
- # Use dictionary operators to get, set and delete rows from the LSM
- # table. Slices may be passed to represent a range of key values.
- def get_timestamp():
- # Return time as integer expressing time in microseconds.
- return int(time.time() * 1000000)
- # Create a new row, at current timestamp.
- ts = get_timestamp()
- EventLog[ts] = ('pageview', 'search', '/blog/some-post/')
- # Retrieve row from event log.
- log = EventLog[ts]
- print(log.action, log.sender, log.target)
- # Prints ("pageview", "search", "/blog/some-post/")
- # Delete the row.
- del EventLog[ts]
- # We can also use the "create()" method.
- EventLog.create(
- timestamp=get_timestamp(),
- action='signup',
- sender='newsletter',
- target='sqlite-news')
Simple key/value model declaration:
- class KV(LSMTable):
- key = TextField(primary_key=True)
- value = TextField()
- class Meta:
- database = db
- filename = 'kv.ldb'
- db.create_tables([KV])
For tables consisting of a single value field, Peewee will return the valuedirectly when getting a single item. You can also request slices of rows,in which case Peewee returns a corresponding Select
query,which can be iterated over. Below are some examples:
- >>> KV['k0'] = 'v0'
- >>> print(KV['k0'])
- 'v0'
- >>> data = [{'key': 'k%d' % i, 'value': 'v%d' % i} for i in range(20)]
- >>> KV.insert_many(data).execute()
- >>> KV.select().count()
- 20
- >>> KV['k8']
- 'v8'
- >>> list(KV['k4.1':'k7.x']
- [Row(key='k5', value='v5'),
- Row(key='k6', value='v6'),
- Row(key='k7', value='v7')]
- >>> list(KV['k6xxx':])
- [Row(key='k7', value='v7'),
- Row(key='k8', value='v8'),
- Row(key='k9', value='v9')]
You can also index the LSMTable
using expressions:
- >>> list(KV[KV.key > 'k6'])
- [Row(key='k7', value='v7'),
- Row(key='k8', value='v8'),
- Row(key='k9', value='v9')]
- >>> list(KV[(KV.key > 'k6') & (KV.value != 'v8')])
- [Row(key='k7', value='v7'),
- Row(key='k9', value='v9')]
You can delete single rows using del
or multiple rows using slicesor expressions:
- >>> del KV['k1']
- >>> del KV['k3x':'k8']
- >>> del KV[KV.key.between('k10', 'k18')]
- >>> list(KV[:])
- [Row(key='k0', value='v0'),
- Row(key='k19', value='v19'),
- Row(key='k2', value='v2'),
- Row(key='k3', value='v3'),
- Row(key='k9', value='v9')]
Attempting to get a single non-existant key will result in a KeyError
,but slices will not raise an exception:
- >>> KV['k1']
- ...
- KeyError: 'k1'
- >>> list(KV['k1':'k1'])
- []
Parameters:length (int) – Size of blob in bytes.
ZeroBlob
is used solely to reserve space for storing a BLOBthat supports incremental I/O. To use the SQLite BLOB-storeit is necessary to first insert a ZeroBlob of the desired size into therow you wish to use with incremental I/O.
For example, see Blob
.
Parameters:
- database –
SqliteExtDatabase
instance. - table (str) – Name of table being accessed.
- column (str) – Name of column being accessed.
- rowid (int) – Primary-key of row being accessed.
- read_only (bool) – Prevent any modifications to the blob data.
Open a blob, stored in the given table/column/row, for incremental I/O.To allocate storage for new data, you can use the ZeroBlob
,which is very efficient.
- class RawData(Model):
- data = BlobField()
- # Allocate 100MB of space for writing a large file incrementally:
- query = RawData.insert({'data': ZeroBlob(1024 * 1024 * 100)})
- rowid = query.execute()
- # Now we can open the row for incremental I/O:
- blob = Blob(db, 'rawdata', 'data', rowid)
- # Read from the file and write to the blob in chunks of 4096 bytes.
- while True:
- data = file_handle.read(4096)
- if not data:
- break
- blob.write(data)
- bytes_written = blob.tell()
- blob.close()
Parameters:n (int) – Only read up to n bytes from current position in file.
Read up to n bytes from the current position in the blob file. If _n_is not specified, the entire blob will be read.
Parameters:
- **offset** (_int_) – Seek to the given offset in the file.
- **whence** (_int_) – Seek relative to the specified frame of reference.
Values for whence
:
- <code>0</code>: beginning of file
- <code>1</code>: current position
- <code>2</code>: end of file
Parameters:data (bytes) – Data to be written
Writes the given data, starting at the current position in the file.
Parameters:rowid (int) – Primary key of row to open.
If a blob has already been opened for a given table/column, you can usethe reopen()
method to re-use the same Blob
object for accessing multiple rows in the table.