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:

  1. from pony.orm import *
  2. db = Database()
  3. class Product(db.Entity):
  4. id = PrimaryKey(int, auto=True)
  5. name = Required(str)
  6. stars = Optional(IntArray)
  7. tags = Optional(StrArray)
  8. db.bind('sqlite', ':memory:')
  9. db.generate_mapping(create_tables=True)
  10. with db_session:
  11. 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.