-- unique_field: projectID:userID:grantID (project.NewAddProjectGrantMemberUniqueConstraint) -- resource_owner is the project org; also stamp the user's home org and the granted org UPDATE eventstore.unique_constraints uc SET owners = ARRAY['org:' || m.resource_owner, 'org:' || m.user_resource_owner, 'user:' || m.user_id, 'project:' || m.project_id] || CASE WHEN COALESCE(m.granted_org, '') <> '' THEN ARRAY['org:' || m.granted_org] ELSE ARRAY[]::text[] END || CASE WHEN COALESCE(m.grant_id, '') <> '' THEN ARRAY['grant:' || m.grant_id] ELSE ARRAY[]::text[] END FROM {{.project_grant_members}} m WHERE uc.unique_type = 'project_grant_member' AND uc.instance_id = m.instance_id AND uc.owners = '{}' AND uc.unique_field IN (m.project_id || ':' || m.user_id || ':' || m.grant_id, lower(m.project_id || ':' || m.user_id || ':' || m.grant_id));