fireproofsocks

fireproofsocks

There is a nice table showing the available data types used in Ecto Schemas: Ecto.Schema — Ecto v3.14.0

But I cannot find a corresponding list that enumerates the possibilities available to an Ecto Migration. They are similar, but they are not always the same. Does anyone have such a list? And should that be a part of the docs?

Many thanks!

Showing Posts 1 to 10

fireproofsocks

fireproofsocks OP

That’s the link I meant to include in my post (now edited). That lists the data types for SCHEMAS. It’s similar, but not the same for MIGRATIONS. Thus the post. Where is there a similar list for types available within migrations?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This depends on the underlying database adapter. You can always use fragment for the type as well and specify anything you like.

fireproofsocks

fireproofsocks OP

I figured out how to take advantage of PostGreSQL’s inet data type following Pedro’s excellent article:

add :ip, :inet, null: false, default: fragment("'0.0.0.0'::inet")

Note that you can just put whatever PostGres data type as the 2nd argument (no need to use fragment there). The fragment macro was however useful to supply a default argument for that field.

There is still the correlating types used by the schema, but this gets me so I’m able to create the database the way I want. Thanks all!

kip

kip

ex_cldr Core Team

The list of types defined for Postgres is in postgrex extensions.

DidactMacros

DidactMacros

Seems :string is not included in this list. I know bit_string is here, but the form used in ecto migrations is :string. Just an FYI for other readers.

garrison

garrison

There is a mapping from Ecto aliases (like :string) to database types. For Postgres I believe that mapping is right here. So as you can see :string is converted to :varchar. If the type is unknown it is converted to a string and then sent verbatim, which is why you can use any db-specific type (like :citext) even if it’s not present there.

Note that the :string type is not equivalent to :text with Ecto/Postgres, because for some reason :string actually becomes varchar(255) - that is to say it has a limit of 255 characters.

I have no idea where that default size is set (or why, for that matter).

DidactMacros

DidactMacros

That’s a great link you provided.

One might have to rummage through (ctlr f) for mappings for things like :text. It seems the preference is to phx.gen.schema, as access to full schema mappings is readily available.

jswanner

jswanner

255 is an old suggested default size for varchar, there were reason for it at the time, there’s no great reason to continue to use it anymore. For Postgres, unless you want to enforce some hard limit on the size of a string field, it’s best to use text as your default character field type.

kip

kip

ex_cldr Core Team

Another reason to use text is that Postgres string functions typically define their parameters as text, and hence you’re less likely to get type mismatch errors on them. varchar is treated somewhat as a domain over text and hence not exactly the same.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews