I’m currently trying to use Postgres’s identity columns for auto-incrementing primary keys, instead of serial, but I’m unsure how to get it to work properly. First, I tried setting it in the config with
But that, unfortunately, causes any foreign keys to that resource to also be identity columns (here’s the SQL from a migration to create a table with a reference to another).
"post_id" bigint GENERATED ALWAYS AS IDENTITY NOT NULL CONSTRAINT "votes_post_id_fkey" REFERENCES "posts"("id")
I also tried setting the type manually in the migration with
create table(:posts, primary_key: false) do
add :id, :"bigint GENERATED ALWAYS AS IDENTITY", primary_key: true, null: false
end
But that causes a postgrex error probably because of the extra stuff after the column type. I could manually do it with execute, but that’s a lot of work and not an ideal solution.
(Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near "ALWAYS"
because it puts the text DEFAULT before the fragment and GENERATED ALWAYS AS IDENTITY is not a value, but an option for a column (see the where column_constraint is: section of PostgreSQL: Documentation: 10: CREATE TABLE).
@Un3qual, is it resolved? I am trying to run the same but via execute.
I am always getting postgres error, i understand its not a ecto issue, but how to add generated columns? No examples on this at ecto docs.
I am trying to run this:
ALTER TABLE USERS ALTER COLUMN AGE ADD GENERATED ALWAYS AS (extract year from age(now(), dob)) STORED; => error
ALTER TABLE USERS DROP COLUMN AGE;
ALTER TABLE USERS ADD COLUMN AGE TYPE INT GENERATED ALWAYS AS (extract year from age(now(), dob)) STORED; => error
Error:
* (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at (2 errors)
1. or near "INT"
2. or near "GENERATED"