Gateway_profile_id foriegn key violation

Hi brocaar,

I am trying to create gateway_profile in my network server, while doing that everytime i am flushing the complete postgres database, then I am creating loraserver_ns and loraserver_as database.
I am creating completely new network server, service_profile, organization, device_profile, gateway_profile etc.

After creating the above, sometimes I am getting error related to postgres. Refer postgres_error.jpg image for the error we are facing

Since, I am creating gateway_profile with new id somewhere it is getting recognized by old gateway_profile_id which had been already deleted. Eventhough both loraserver_ns and _as has been updated with new profile_id as shown below.

loraserver_as=> select FROM gateway_profile;
gateway_profile_id | network_server_id | created_at | updated_at | name
--------------------------------------±------------------±------------------------------±------------------------------±--------
58469fde-62e5-48d2-a07f-ecd0c88e7f3a | 1 | 2020-01-30 05:21:29.863767+00 | 2020-01-30 05:21:29.863767+00 | LoRa GW
(1 row)
**loraserver_as=> **

loraserver_ns=>
loraserver_ns=> select FROM gateway_profile;
gateway_profile_id | created_at | updated_at | channels
--------------------------------------±------------------------------±------------------------------±---------
58469fde-62e5-48d2-a07f-ecd0c88e7f3a | 2020-01-30 05:21:29.886979+00 | 2020-01-30 05:21:29.886979+00 | {0,1}
(1 row)
loraserver_ns=>

loraserver_ns=> select * FROM gateway;
gateway_id | created_at | updated_at | first_seen_at | last_seen_at | location | altitude | gateway_profile_id | routing_profile_id

--------------------±------------------------------±------------------------------±--------------±-------------±---------±---------±-------------------------------------±---------------------------

\xaa555a0000000101 | 2020-01-30 05:21:42.611363+00 | 2020-01-30 05:21:42.611363+00 | | | (0,0) | 0 | 58469fde-62e5-48d2-a07f-ecd0c88e7f3a | 6d5db27e-4ce2-4b2b-b5d7-91f
069397978
(1 row)

loraserver_ns=> select gateway_profile_id FROM gateway;
gateway_profile_id

58469fde-62e5-48d2-a07f-ecd0c88e7f3a
(1 row)

loraserver_ns=>

Aso I had cross checked with LoRa App Server REST API. There also it was proper.

Because of above behavior , I am getting the error mentioned in postgres_error.jpg

I guess old profile_id has been recognized somewhere. Not sure how to solve this.
old gateway_profile_id: 37e655fd-d3f7-4464-a768-a0150c34cef0

gateway_profile_id currently registered: 58469fde-62e5-48d2-a07f-ecd0c88e7f3a

One more is, this kind of behavior is not repetative, only sometimes it is giving this error after construction. So, I could not able to crack where exactly it is giving this error.

Please let me know if the procedure done for construction is proper. If something is done wrong, let me know.