Sample Header Ad - 728x90

How to update a foreign key with random values from the referenced table?

2 votes
2 answers
722 views
In order to generate test data for a Postgres database, I want to update a foreign key in a table, let's say 'users' with random values from the referenced table, let's say 'shift'. How to do it? I have tried this way:
UPDATE users
SET id_shift = (
SELECT shift.id_shift
FROM shift
ORDER BY random()
LIMIT 1
);
I expected to have some users in one shift and others in another. But the result is that all users end up with the same shift.
Asked by Edilson Vieira (21 rep)
May 25, 2023, 03:03 PM
Last activity: Nov 29, 2024, 08:49 PM