CREATE DATABASE Fails in Nova Resource Due to DB Transaction #6868
Unanswered
Athis-oot
asked this question in
Ideas & Feature Requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
While trying to create a Tenant resource via Laravel Nova, I encountered an issue caused by Nova wrapping the entire resource creation process in a database transaction.
In our multi-tenant setup, creating a new tenant involves executing a CREATE DATABASE statement. However, PostgreSQL does not allow CREATE DATABASE to be run inside a transaction block. Since Nova executes model creation within a transaction (DB::transaction()), the CREATE DATABASE command fails with the following error:
pgsql
Copy
Edit
SQLSTATE[25001]: Active sql transaction: 7 ERROR: CREATE DATABASE cannot run inside a transaction block (Connection: pgsql, SQL: CREATE DATABASE "tenant74ba7ed7-b25e-4c7a-91f9-7a95e3d58a44" WITH TEMPLATE=template0)
This issue occurs because the database creation logic (inside the Tenant model or related service) is being executed within the transaction Nova starts during resource creation.
Expected Behavior:
There should be a way to bypass or disable the automatic transaction wrapping in Nova for specific resources or actions, especially when non-transactional queries like CREATE DATABASE are involved.
Additional Context:
Laravel: 11
Laravel Nova: 5
SaaS boilerplates seem to handle this situation correctly by avoiding transactions or deferring the CREATE DATABASE call.
Potential Workaround (Not Ideal):
It may be possible to manually create the database outside the Nova flow and then use Nova to manage metadata, but that defeats the purpose of automating tenant creation.
Request:
Is it possible to introduce a way to opt-out of automatic transaction handling in Nova for specific resources or operations?
Beta Was this translation helpful? Give feedback.
All reactions