Ecto.Changeset.check_constraint

I have an existing migration Posts

One post has many comments. It is structured as follows.

id
post_name
post_num
comments
      [  
        %{
        id
        post_num
        },
    %{
        id
        post_num
        },
       ]

I would like to add a constraint that checks if the post_num in Posts matches the post_num in comments, if not it should return an error changeset. I came across Ecto.Changeset.check_constraint from the docs. I feel this will be more suitable for my scenario here (would appreciate any other better options aswell). I would like to know how to create a migration for this case and what goes into the migration file.

Could you please post your Post and Comment schema and migration files? It’ll be easier to understand your intention with them, because it seems that you want post_num in comments to refer to Post’s post_num and not its primary id, which would get you the referential integrity you seem to be looking for from the DB.

Okay, so I rechecked my requirements and I realized I would want the post_num in Posts to match the primary id of the comments. And I do not have a post_num field in comments.