Declaring an Array attribute
Each array should have a specified type of items that this array can store. Supported types are: int
, float
and str
.
For declaring an Array attribute with Pony you should use one of the IntArray
, FloatArray
or StrArray
types. These types can be imported from pony.orm
package:
from pony.orm import *
db = Database()
class Product(db.Entity):
id = PrimaryKey(int, auto=True)
name = Required(str)
stars = Optional(IntArray)
tags = Optional(StrArray)
db.bind('sqlite', ':memory:')
db.generate_mapping(create_tables=True)
with db_session:
Product(name='Apple MacBook', stars=[0, 2, 0, 5, 10], tags=['Apple', 'Notebook'])
Note
Optional arrays are declared as NOT NULL by default and use empty array as default value. To make it nullable you should pass nullable=True
option.
Note
For PostgreSQL if you set index=True
Pony will create gin index. For SQLite index will be ignored.