ourway

ourway

Hi there,
I think Ecto development should consider this recommendations:

Showing Posts 32 to 23

Kurisu

Kurisu

By any does someone have a custom Ecto type or something to do that right out of the box?^^

I mean by setting an error like “too long entry” into the changeset instead of throwing the exception

ERROR 22001 (string_data_right_truncation) value too long for type character varying(255)

thousandsofthem

thousandsofthem

Not really necessary to be in sync, db constraint is “safety net” - works when everything else fails.
So, 100 symbols in validation and 255 in database is perfectly valid combo.

cnck1387

cnck1387

Right, that’s something I always think to myself when I develop a “for real” public site.

I’ll attach a maxlength to the input box, followed by a max (and potentially min) length with a changeset validation, followed by enforcing a text + constraint at the database level and finish it up with a test to ensure the changeset validates the length. Without all 4 (first one is mainly a UI hint) I feel really uneasy, but at the same time, it’s quite a bit of duplication and remembering to keep things in sync if one of those lengths change later.

OvermindDL1

OvermindDL1

Don’t quote me, but as I recall it sets the new constraint and wipes the old one atomically (and linearly, I.E. it can take time), and depending on set option it will either truncate or error at that time if anything is too long then.

cnck1387

cnck1387

I’ve always done the text + constraint manually which works well enough but I’ll admit, it’s a tiny bit inconvenient to always add it to your migration.

If you use varchar(N), what happens if you change N down the line? Does pg know to modify the underlying constraint’s length behind the scenes?

sribe

sribe

Depends on context. If you’re writing a public-facing application, yes, absolutely. Private in-house apps are a whole different issue…

thousandsofthem

thousandsofthem

If you use “text” everywhere and don’t always remember to set limits in validations expect someone to have 1GB username

hauleth

hauleth

Which will be pretty irritating for the end user as they will not know what is wrong. Which can be in any place from mildly irritating to that cannot ever happen and we resign from your services depending on what your application does.

Rarely, and even when I do then I do not treat it as “this limit will never change” as something like that is highly improbable. Twitter changed character limit, Git will change character limit, etc. Setting hard limit because right now you think that it will be set in stone isn’t wise. So application-side check is all you need, you do not need to impose limit on the DB as well.

Yes, that is big. But if you want to save 342 copies of whole War and Peace in one text field then I would expect bigger problems before it would even hit the DB.

Cochonours

Cochonours

Then you never validate your string length? If you do, this argument doesn’t hold. If you don’t, then to each his own I guess.

Would only happen with some extremely uncommon combination of new unicode code points, and the only risk is having a DB error instead of the changeset validation error, so it wouldn’t even be irritating for anyone (a user playing around with the weirdest of strings would not complain).
If you really want to take into account such kind of unlikely scenario, then you should always define a max length as there is a risk your string will be too long to store (“there is always a bigger fish”) as even text fields always have a hard coded max size (1GB for Postgres ; that’s a big fish).

hauleth

hauleth

As I said earlier, you almost never know the size of the text beforehand. “There is always a bigger fish” as it was said and almost any character limit will be lifted sooner or later.

Also as I said - problem with characters counting. For example imagine that your DB doesn’t adapt new Unicode standard or it was not yet updated to use newer tables. Now you can get counting mismatches between your application and DB which will lead to irritating errors

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews