<@U04UQ68083H> Does Ory Keto have a way of prevent...
# ory-copilot
r
@chilly-king-10285 Does Ory Keto have a way of preventing creation of duplicate relationship definitions? Through the API, it seems that the "Create Relationship" endpoint can be called multiple times with the same request, resulting in multiple entries being created
b
A message from kapa.ai
Based on the knowledge sources provided, I can answer your question about Ory Keto's handling of duplicate relationship definitions. Ory Keto does not have a built-in mechanism to prevent the creation of duplicate relationship definitions through its API. The "Create Relationship" endpoint (
PUT /admin/relation-tuples
) can indeed be called multiple times with the same request, potentially resulting in duplicate entries. However, Ory provides alternative methods that can help manage this situation: 1. Use the PATCH endpoint: Instead of using the PUT endpoint to create relationships, you can use the PATCH endpoint (
PATCH /admin/relation-tuples
) which allows for more granular control. This endpoint accepts an array of actions, where each action can be either "insert" or "delete". Using "insert" will only create the relationship if it doesn't already exist, effectively preventing duplicates. [Keto API Reference](https://www.ory.sh/docs/keto/reference/rest-api) provides more details on this endpoint. 2. Use the transaction-based methods: The Ory documentation recommends using transaction-based methods for bulk updates. As stated in the [API overview](https://www.ory.sh/docs/keto/concepts/api-overview): "It's preferred to use the transaction based methods over repeatedly calling simple methods for bulk updates. This isn't only because they provide stronger consistency guarantees, but also because the database usually handles a single transaction with a lot of data faster than a lot of small transactions." While these methods don't explicitly prevent duplicates, they provide more control over the creation process and can be used to implement duplicate-checking logic on the client side. It's worth noting that the exact behavior might depend on the underlying database implementation, but based on the provided information, Ory Keto itself does not have a built-in mechanism to prevent duplicate relationships created through repeated API calls.