Seed Identity Operation



Set a higher identity value

You can replace the latest identity value on the server with a new, higher number.

# Seed a higher identity value on the server:
# ===========================================

# Define the seed identity operation. Pass:
# * The collection name (can be with or without a pipe)
# * The new value to set
seed_identity_op = SeedIdentityForOperation("companies|", 23)

# Execute the operation by passing it to maintenance.send
# The latest value on the server will be incremented to "23"
# and the next document created with an identity will be assigned "24"
seeded_value = store.maintenance.send(seed_identity_op)

# Create a document with an identity ID:
# ======================================

with store.open_session() as session:
    session.store(Company(name="RavenDB"), "companies|")
    session.save_changes()
    # => Document "companies/24" will be created

Force a lower identity value

  • You can set the latest identity value to a number that is lower than the current latest value.

  • Before proceeding, first ensure that documents with an identity value higher than the new number do not exist.

# Force a smaller identity value on the server:
# =============================================

# Define the seed identity operation. Pass:
#   * The collection name (can be with or without a pipe)
#   * The new value to set
#   * Set 'force_update' to True
seed_identity_op = SeedIdentityForOperation("companies|", 5, force_update=True)

# Execute the operation by passing it to maintenance.send
# The latest value on the server will be decremented to "5"
# and the next document created with an identity will be assigned "6"
seeded_value = store.maintenance.send(seed_identity_op)

# Create a document with an identity ID:
# ======================================

with store.open_session() as session:
    session.store(Company(name="RavenDB"), "companies|")
    session.save_changes()
    # => Document "companies/6" will be created

Syntax

class SeedIdentityForOperation(MaintenanceOperation[int]):
    def __init__(self, name: str, value: int, force_update: bool = False): ...
Parameter Type Description
name str The collection name to seed the identity value for.
Can be ended with or without a pipe (e.g. "companies" or "companies|".
value long The number to set as the latest identity value.
force_update bool True - force a new value that is lower than the latest.
False - only a higher value can be set.