File: unique_field.py

package info (click to toggle)
python-odmantic 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,488 kB
  • sloc: python: 8,646; sh: 110; javascript: 45; makefile: 34; xml: 13
file content (18 lines) | stat: -rw-r--r-- 458 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from odmantic import AIOEngine, Field, Model


class Player(Model):
    name: str = Field(unique=True)


engine = AIOEngine()
await engine.configure_database([Player])

leeroy = Player(name="Leeroy")
await engine.save(leeroy)

another_leeroy = Player(name="Leeroy")
await engine.save(another_leeroy)
#> Raises odmantic.exceptions.DuplicateKeyError:
#>    Duplicate key error for: Player.
#>    Instance: id=ObjectId('6314b4c25a19444bfe0c0be5') name='Leeroy'