Creating databases and tables using the postgresql module in Ansible is pretty straight forward and works great, but I can´t really figure out how to create a foreign key constraint.
Eg. I create a table in Ansible like this:
- name: "Create foo table"
become: yes
become_user: postgres
community.postgresql.postgresql_table:
db: test_db
owner: postgres
name: foo
columns:
- id bigserial PRIMARY KEY NOT NULL
- bar_id bigint
Now I would like to create a foreign key constraint that references the id column in the table bar. In Postgresql I could add a foreign key constrain this like this:
ALTER TABLE foo ADD CONSTRAINT bar_id_fkey FOREIGN KEY (bar_id) REFERENCES bar (id) MATCH FULL;
How can this be done in Ansible?