Executing Multi-Tenant Aware Queries in Bootstrap.groovy

565 Views Asked by At

While trying to save some data to the datastore belonging to a specific tenant in BootStrap.groovy I am getting the following error.

org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException: Tenant could not be resolved outside a web request

I am using SessionTenantResolver as tenantResolver class config in application.yml

  multiTenancy:
                mode: DATABASE
                tenantResolverClass: org.grails.datastore.mapping.multitenancy.web.SessionTenantResolver

I have tried wrapping the tenant aware query inside withId as given below

withId("tenant1") {
    User tenant1Admin = new User()
    tenant1Admin.username = "tenaant1admin"
    tenant1Admin.password = "password"
    tenant1Admin.save(flush:true)
}

User.groovy (generated by s2-quickstart)

class User implements MultiTenant<User>, Serializable {
...
}

What I am trying to achieve is to store super admin related data to the default datastore and other admins/users auth info to the datastore of the tenant they belong to.

Also, if I create only the super admin from Bootstrap.groovy and save it to the default data store, how should I be doing it?

0

There are 0 best solutions below