fusionauth-issues icon indicating copy to clipboard operation
fusionauth-issues copied to clipboard

[Bug]: See an FK violation error message on raw_logins

Open mooreds opened this issue 2 weeks ago • 0 comments

What happened?

If you insert and then immediately remove a user from FusionAuth, you get an error message, see below.

Here's the excerpt of the script I use to add and remove the user.

        // Check 2: API Key permissions by creating and deleting a test admin user
        console.log(chalk.cyan('\n2. Checking API key permissions...'));
        try {
            const testEmail = `test-admin-${Date.now()}@fusionauth-cli-check.local`;
            const testPassword = `TestPass-${Date.now()}-!@#$`;
            
            // Create test user
            const createUserResponse = await fusionAuthClient.createUser(null!, {
                user: {
                    email: testEmail,
                    password: testPassword,
                    username: `test_admin_${Date.now()}`
                }
            });

            if (!createUserResponse.wasSuccessful()) {
                results.push({
                    passed: false,
                    message: 'API key lacks permissions to create users'
                });
                allPassed = false;
            } else if (!createUserResponse.response.user?.id) {
                results.push({
                    passed: false,
                    message: 'Created user but no user ID returned'
                });
                allPassed = false;
            } else {
                const userId = createUserResponse.response.user.id;
                
                // Search for the FusionAuth application
                const searchAppsResponse = await fusionAuthClient.searchApplications({
                    search: {
                        name: 'FusionAuth'
                    }
                });

                if (!searchAppsResponse.wasSuccessful() || !searchAppsResponse.response.applications || searchAppsResponse.response.applications.length === 0) {
                    results.push({
                        passed: false,
                        message: 'Could not find FusionAuth application'
                    });
                    allPassed = false;
                    // Clean up
                    await fusionAuthClient.deleteUser(userId);
                } else {
                    const fusionAuthApp = searchAppsResponse.response.applications[0];
                    
                    if (!fusionAuthApp.id) {
                        results.push({
                            passed: false,
                            message: 'FusionAuth application has no ID'
                        });
                        allPassed = false;
                        // Clean up
                        await fusionAuthClient.deleteUser(userId);
                    } else {
                        // Register user to FusionAuth app with admin role
                        const registrationResponse = await fusionAuthClient.register(userId, {
                            registration: {
                                applicationId: fusionAuthApp.id,
                                roles: ['admin']
                            }
                        });

                        // Clean up - delete test user
                        await fusionAuthClient.deleteUser(userId);

                        if (!registrationResponse.wasSuccessful()) {
                            results.push({
                                passed: false,
                                message: 'API key lacks permissions to register users with admin role'
                            });
                            allPassed = false;
                        } else {
                            results.push({
                                passed: true,
                                message: 'API key has appropriate permissions ✓'
                            });
                        }
                    }
                }
            }
        } catch (e) {
            results.push({
                passed: false,
                message: `API key permission check failed: ${e instanceof Error ? e.message : String(e)}`
            });
            allPassed = false;
        }

Should raw_logins have an FK to the users table? Seems weird.

fusionauth-1  | ### Error updating database.  Cause: org.postgresql.util.PSQLException: ERROR: insert or update on table "raw_logins" violates foreign key constraint "raw_logins_fk_2"
fusionauth-1  |   Detail: Key (users_id)=(4d840566-a2ea-4d66-b1c5-a3974c5830e9) is not present in table "users".
fusionauth-1  | ### The error may exist in io/fusionauth/api/domain/LoginMapper.xml
fusionauth-1  | ### The error may involve defaultParameterMap
fusionauth-1  | ### The error occurred while setting parameters
fusionauth-1  | ### SQL: INSERT INTO raw_logins (applications_id, instant, ip_address, identities_value, identities_type, users_id) VALUES              (?, ?, ?, ?, ?, ?)
fusionauth-1  | ### Cause: org.postgresql.util.PSQLException: ERROR: insert or update on table "raw_logins" violates foreign key constraint "raw_logins_fk_2"
fusionauth-1  |   Detail: Key (users_id)=(4d840566-a2ea-4d66-b1c5-a3974c5830e9) is not present in table "users".
fusionauth-1  | 	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
fusionauth-1  | 	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199)
fusionauth-1  | 	at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)
fusionauth-1  | 	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
fusionauth-1  | 	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
fusionauth-1  | 	at org.apache.ibatis.session.SqlSessionManager$SqlSessionInterceptor.invoke(SqlSessionManager.java:355)
fusionauth-1  | 	at jdk.proxy2/jdk.proxy2.$Proxy70.insert(Unknown Source)
fusionauth-1  | 	at org.apache.ibatis.session.SqlSessionManager.insert(SqlSessionManager.java:234)
fusionauth-1  | 	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62)
fusionauth-1  | 	at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:141)
fusionauth-1  | 	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
fusionauth-1  | 	at jdk.proxy2/jdk.proxy2.$Proxy109.createRawLogins(Unknown Source)
fusionauth-1  | 	at io.fusionauth.api.service.authentication.LoginQueue$LoginThread.run(LoginQueue.java:309)
fusionauth-1  | Caused by: org.postgresql.util.PSQLException: ERROR: insert or update on table "raw_logins" violates foreign key constraint "raw_logins_fk_2"
fusionauth-1  |   Detail: Key (users_id)=(4d840566-a2ea-4d66-b1c5-a3974c5830e9) is not present in table "users".
fusionauth-1  | 	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2734)
fusionauth-1  | 	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2421)
fusionauth-1  | 	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:372)
fusionauth-1  | 	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:518)
fusionauth-1  | 	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:435)
fusionauth-1  | 	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:196)
fusionauth-1  | 	at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:182)
fusionauth-1  | 	at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
fusionauth-1  | 	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
fusionauth-1  | 	at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:48)
fusionauth-1  | 	at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:75)
fusionauth-1  | 	at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
fusionauth-1  | 	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
fusionauth-1  | 	at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
fusionauth-1  | 	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
fusionauth-1  | 	... 11 common frames omitted

Version

1.61.2

Affects Versions

No response

Alternatives / Workarounds

Haven't tested this, but I assume a sleep between the add and the delete would not cause the error message.

mooreds avatar Dec 13 '25 18:12 mooreds