KP123

KP123

Postgrex regular expression error

Problem: I receive (Postgrex.Error) Error 2201B (invalid_regular_expression) invalid regular expression: brackets [] not balanced for a constraint on one of my tables despite the regex running as expected in tests raw SQL in the Postgres shell.

Context: Im building a table for message attachments. The attachments can have a filename, that field has a constraint to ensure it matches the form of a filename in my app. The regex I’m using is [^\\]*\.(\w+)$ which admittedly I sourced from this stack overflow post.

What I’ve tried: I tested the regex on regex101 and in the Postgres shell and it worked as intended. The problem is in my migration, I know this because when I remove the constraint everything works perfectly.

I also tried the solution in this DBA stack exchange post.

Simplified table declaration:

defmodule MyApp.Repo.Migrations.CreateAttachmentsTable do
  use Ecto.Migration

  @filename_with_extension_regex "[^\\]*\.(\w+)$"

  def change do
    create table(:attachments) do
      add :filename, :string, null: false
      add :message_id, references(:messages, on_delete: :delete_all), null: false

      timestamps(created_at: false, updated_at: false) # Takes the time signature of the message
    end

    create unique_index(:attachments, [:filename, :message_id], name: :attachments_filename_message_id_index)
    create constraint(:attachments, :filename_must_be_valid_filepath, check: "filename ~* '#{@filename_with_extension_regex}'")
  end
end

Marked As Solved

sodapopcan

sodapopcan

It’s because you’re in Elixir quotes which use backslash to escape, so if you want a backslash character you have to escape it. This works:

@filename_with_extension_regex "[^\\\\]*\\.(\\w+)$"

If that is too ugly you could also use ~S:

@filename_with_extension_regex ~S"[^\\]*\.(\w+)$"

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement