I am using :string as column type in a migration. But the generated raw SQL is varchar(255) rather than varchar which is declared at here.
My environment:
PostgreSQL 12
ecto 3.4.5
ecto_sql 3.4.4
I created a minimal migraiton like this:
defmodule SampleApp.Repo.Migrations.Test do
use Ecto.Migration
def change do
create table("test_types") do
add :string, :string, null: false
add :varchar, :varchar, null: false
add :text, :text, null: false
timestamps()
end
end
end
Then, I inspected the generated SQL with mix ecto.migrate --log-sql, and the output is:
CREATE TABLE "test_types" (
"id" bigserial,
"string" varchar(255) NOT NULL,
"varchar" varchar NOT NULL,
"text" text NOT NULL,
"inserted_at" timestamp(0) NOT NULL,
"updated_at" timestamp(0) NOT NULL, PRIMARY KEY ("id")) []
As you see:
string -> varchar(255) [BAD]
varchar -> varchar [GOOD]
text -> text [GOOD]
After searching on source code of ecto and ecto_sql, I still have no idea why :string is not mapped to varchar but varchar(255).
For postgres there is no difference between text and an unsized varchar so I think it’s mostly about making that distinction clear, because the only time to use varchar is when you want to set the size.