Bug in getting key after registration

First of all, I think there is an incorrect design in popstore.CustomerServices.register#Customer [1] as it should login immediately after creating a user, otherwise the code in the rest of the service will not execute. Anyway, when I correct this logic by setting loginAfterCreate: true I immediately face an exception [3]. This exception is coming from the line that tries to get the user login key [2]

This is not the first time I face such bugs. There seems to be some bugs in the framework around making consecutive calls to the entity engine.

Any thoughts on how to resolve this?

[1] <service-call name="mantle.party.PartyServices.create#Account" in-map="context + [roleTypeId:'Customer', loginAfterCreate:false]"/>

[2] <set field="apiKey" from="ec.user.getLoginKey()"/>


20:09:35.852  WARN 112797691-42            o.moqui.i.e.EntityValueImpl Error creating [moqui.security.UserLoginKey: [loginKey:a36fde8ad2199983287c18d3ebd0cf65733d5b4aea470cc64525e5b80f898bc7, userId:100153, fromDate:2021-06-12 20:09:35.85, thruDate:2021-06-18 20:09:35.85, null:null, lastUpdatedStamp:2021-06-12 20:09:35.85]] tx a Bitronix Transaction with GTRID [3132372E302E302E310000017A013347EA00000015], status=ACTIVE, 1 resource(s) enlisted (started Sat Jun 12 20:09:35 AST 2021) con Group: transactional, Con: a ConnectionJavaProxy of a JdbcPooledConnection from datasource transactional_DS in state ACCESSIBLE with usage count 1 wrapping org.postgresql.xa.PGXAConnection@189b5fb1 on Pooled connection wrapping physical connection org.postgresql.jdbc.PgConnection@4510ddaa: org.postgresql.util.PSQLException: ERROR: insert or update on table "user_login_key" violates foreign key constraint "userloginkeyuseraccount"
  Detail: Key (user_id)=(100153) is not present in table "user_account".

From the error it looks like the UserAccount record was not created (inserted). Is this what you mean by “some bugs in the framework around making consecutive calls to the entity engine”?

Without steps to reproduce (to see exact code, exact behavior) it is difficult to say more, but one common issue with a symptom like a record not existing is splitting logic across multiple transactions (require new TX or async).

In steps to reproduce one important detail is which version of moqui-framework and other components you are using. More generally, steps to reproduce are steps that you are requesting that someone else follow, someone who you are requesting help from and who only has the information you include and the open source code and config.

1 Like

Hi David, so yes the UserAccount is not “yet” created as it seems, but the entire code is under a single service, so it would seem strange to not have everything wrapped with one transaction.

The steps to reproduce:

  1. Prepare the system with PopRestStore component included
  2. Make sure your database is postgresql (I didn’t test with H2)
  3. Edit popstore.CustomerServices.register#Customer and change loginAfterCreate value from false to true
  4. Try to register, for example you can run a curl [1] command
  5. Observe the mentioned exception


curl -X POST \
    --header 'Content-Type: application/json' \
    --header 'Accept: application/json' \
    -d '{"firstName":"FirstHere",
         "emailAddress":"[email protected]",
         "username":"[email protected]",
         "timeZone":""}' \