I have a 1 to many Organization: Users relationship.
I want to fetch the usernames of all User model of an Organization, capture a part of that username and append/substitute it with new value.
Here is how I am doing:
- Form the raw SQL to Get the matching usernames and replace them with new value.
raw = "SELECT REGEXP_REPLACE($1::string[], '(^[a-z0-9]+)((\s[a-z0-9]+))*\@([a-z0-9]+)$', m.name[1] || '@' || $2) FROM (SELECT REGEXP_MATCHES($1::string[], '(^[a-z0-9]+)((\s[a-z0-9]+))*\@([a-z0-9]+)$') AS name) m"
- Get the matching usernames and replace them with new value.
usernames: list of usernames retrieved from queryable
Repo.query(raw, [usernames, a_string])
-
Error I am getting
SELECT REGEXP_REPLACE($1::string[], ‘(^[a-z0-9]+)(( [a-z0-9]+))@([a-z0-9]+)$’, m.name[1] || ‘@’ || $2) FROM (SELECT REGEXP_MATCHES($1::string[], '(^[a-z0-9]+)(( [a-z0-9]+))@([a-z0-9]+)$’) AS name) m [[“tradeboox@trdbx18”], “trdbx17”]
{:error,
%Postgrex.Error{connection_id: 7222, message: nil,
postgres: %{code: :undefined_object, file: “parse_type.c”, line: “257”,
message: “type “string[]” does not exist”, pg_code: “42704”,
position: “137”, routine: “typenameType”, severity: “ERROR”,
unknown: “ERROR”}}}
FYI: The username
field of User model is of type citext
- Once I get the replaced values, I want to update the User with something like
update([u], set: [username: new_values])
Any ideas on how to proceed with this.
`