Seed Identity Operation
-
Use
SeedIdentityForOperation
to set the latest identity value for the specified collection. -
The next document that will be created using an identity for the collection will receive the consecutive integer value.
-
Identity values can also be managed from the Studio identities view.
-
In this page:
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
$seedIdentityOp = new 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"
$seededValue = $store->maintenance()->send($seedIdentityOp);
// Create a document with an identity ID:
// ======================================
$session = $store->openSession();
try {
$company = new Company();
$company->setName("RavenDB");
$session->store($company, "companies|");
$session->saveChanges();
// => Document "companies/24" will be created
} finally {
$session->close();
}
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 'forceUpdate' to true
$seedIdentityOp = new SeedIdentityForOperation("companies|", 5, forceUpdate: 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"
$seededValue = $store->maintenance()->send($seedIdentityOp);
// Create a document with an identity ID:
// ======================================
$session = $store->openSession();
try {
$company = new Company();
$company->setName("RavenDB");
$session->store($company, "companies|");
$session->saveChanges();
// => Document "companies/6" will be created
} finally {
$session->close();
}
Syntax
SeedIdentityForOperation(string $name, int $value, bool $forceUpdate = false)
Parameter | Type | Description |
---|---|---|
$name | string |
The collection name to seed the identity value for. Can be ended with or without a pipe (e.g. "companies" or "companies|". |
$value | int |
The number to set as the latest identity value. |
$forceUpdate | bool |
True - force a new value that is lower than the latest.False - only a higher value can be set. |