UserAuthRepository can't find tables

Working on an old project with SS 5.4.
There was a request to move the SS auth tables (user_auth, user_auth_details) to a separate schema.
For whatever reason SS throws the following.

System.Exception: OrmLiteAuthRepository Db tables have not been initialized. Try calling 'InitSchema()' in your AppHost Configure method.

This is on PostgreSQL.
The Auth DB user has been granted access to the schema and tables.
The db user has also included the new auth schema in the search_path however it still seems to throw that error.
The db user via database IDE has no problem accessing the tables. With and without the schema specified.

Tried to override classes with [Schema] attribute with no luck.

In AppHost

container.Register<IAuthRepository>(c =>
            new OrmLiteAuthRepository<MyNewUserAuth, MyNewUserAuthDetails>(
                c.Resolve<IDbConnectionFactory>(), NamedConnectionAuth));

New User Auth Classes

    [Schema("my_new_auth_schema")]
    public class MyNewUserAuth : UserAuth
    {
        
    }

    [Schema("my_new_auth_schema")]
    public class MyNewUserAuthDetails : UserAuthDetails
    {

    }

Is there a way to inspect or log the query that determines they haven’t been initialized?
Or is there a better way to handle this situation?

Thanks :slight_smile:

Full Exception

2022-02-02 07:15:56.745 +00:00 [ERR] ServiceException: https://www.mywebsite.test/auth/credentials
System.Exception: OrmLiteAuthRepository Db tables have not been initialized. Try calling 'InitSchema()' in your AppHost Configure method.
   at ServiceStack.Auth.OrmLiteAuthRepositoryBase`2.<>c__DisplayClass17_0.<GetUserAuthByUserName>b__0(IDbConnection db) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack.Server\Auth\OrmLiteAuthRepository.cs:line 285
   at ServiceStack.Auth.OrmLiteAuthRepository`2.Exec[T](Func`2 fn) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack.Server\Auth\OrmLiteAuthRepository.cs:line 51
   at ServiceStack.Auth.OrmLiteAuthRepositoryBase`2.GetUserAuthByUserName(String userNameOrEmail)
   at ServiceStack.Auth.OrmLiteAuthRepositoryBase`2.TryAuthenticate(String userName, String password, IUserAuth& userAuth) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack.Server\Auth\OrmLiteAuthRepository.cs:line 359
   at ServiceStack.Auth.CredentialsAuthProvider.TryAuthenticate(IServiceBase authService, String userName, String password) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\CredentialsAuthProvider.cs:line 62
   at ServiceStack.Auth.CredentialsAuthProvider.Authenticate(IServiceBase authService, IAuthSession session, String userName, String password, String referrerUrl) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\CredentialsAuthProvider.cs:line 120
   at ServiceStack.Auth.CredentialsAuthProvider.Authenticate(IServiceBase authService, IAuthSession session, Authenticate request) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\CredentialsAuthProvider.cs:line 97
   at MyCredentialsAuthProvider.Authenticate(IServiceBase authService, IAuthSession session, Authenticate request) in C:\dev\myproject\src\Project1\MyCredentialsAuthProvider.cs:line 111
   at ServiceStack.Auth.AuthenticateService.Authenticate(Authenticate request, String provider, IAuthSession session, IAuthProvider oAuthConfig) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\AuthenticateService.cs:line 411
   at ServiceStack.Auth.AuthenticateService.Post(Authenticate request) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\AuthenticateService.cs:line 218
   at lambda_method(Closure , Object , Object )
   at ServiceStack.Host.ServiceRunner`1.ExecuteAsync(IRequest req, Object instance, TRequest requestDto) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\ServiceRunner.cs:line 133

Hi @cthames,

The error occurs when a TableExists check fails. I put together a quick test example using a different schema for these three tables, which is on GitHub to show it at least working in the latest version. However, looking at the Git history, it is likely this was fixed in this commit after 5.4 was released in Sept 2018.

If you upgrade to a version 5.6+ this should be resolved.

Something to note if you are using UserAuthRole table is to apply the attribute at runtime. Eg:

[Schema("test2")]
public class AppUser : UserAuth
{
    public string? ProfileUrl { get; set; }
    public string? LastLoginIp { get; set; }
    public DateTime? LastLoginDate { get; set; }
}

...

typeof(UserAuthDetails).AddAttributes(new SchemaAttribute("test2"));
typeof(UserAuthRole).AddAttributes(new SchemaAttribute("test2"));
services.AddSingleton<IAuthRepository>(c =>
    new OrmLiteAuthRepository<AppUser, UserAuthDetails>(c.Resolve<IDbConnectionFactory>())
    {
        UseDistinctRoleTables = true
    });

The additional attributes on the UserAuthRole should impact this fix but is something you will want to do as well.

Updated to 5.9.2. Still had the issue, but was able solve the problem.

Note: It’s probable that the update also fixed the Schema bug so that this could be fixed.

The new user auth classes needed to have an [Alias("")] of the current auth table names. Otherwise it was converting class MyNewUserAuth to table name my_new_user_auth ;

    [Schema("my_new_auth_schema")]
    [Alias("user_auth")]
    public class MyNewUserAuth : UserAuth
    {
        
    }

    [Schema("my_new_auth_schema")]
    [Alias("user_auth_details")]
    public class MyNewUserAuthDetails : UserAuthDetails
    {

    }

If anyone else has the issue and for those using OrmLite, you can see the sql output via the following.

https://docs.servicestack.net/ormlite/introspection

AppHost.cs

OrmLiteConfig.BeforeExecFilter = dbCmd => Log.Debug(dbCmd.GetDebugString());

Thanks @layoric for the quick response!

Ahh right, I missed that. Yes, if you didn’t create the tables with utilities like InitSchema so that your auth tables match your UserAuth classes, you’ll need to Alias them yourself.

Since you don’t have any new properties on these tables, you could also use the AddAttributes method to add the [Schema] attribute to the original UserAuth and UserAuthDetails since then the class → table names would match.

Thanks for sharing your fix!