How to create an elixir join table using factories? (Postgrex.Error) ERROR 23502 (not_null_violation) null value in column “inserted_at”

I am attempting to fulfill a test where a user can create a prescription if and only if the user has a role of “Prescription Approval Role” with a permission of “approve_scripts”. In order to fulfill the test, I have the following factories setup using ex_machina for elixir:

      def user_factory do
        %User{
          id: Ecto.UUID.generate(),
          emr_id: Ecto.UUID.generate(),
          username: sequence(:user_username, &"user-#{&1})"),
          name_first: sequence(:user_name_first, &"user-#{&1})"),
          name_middle: sequence(:user_name_middle, &"user-#{&1})"),
          name_last: sequence(:user_name_last, &"user-#{&1})"),
          roles: [build(:role)],
          type: "practice"
        }
      end

      def permission_factory do
        %Permission{
          key: "approve_scripts",
          label: "Prescription Approval Permission",
          description: "Permission to approve scripts"
        }
      end

      def role_factory do
        %Role{
          label: "Prescription Approval Role",
          description: "Role needed to approve prescriptions",
          permissions: [build(:permission)]
        }
      end

Here is the schema that corresponds and maps to my factories:

schema "users" do
    field :emr_id, :string
    field :username, :string
    field :name_first, :string
    field :name_middle, :string
    field :name_last, :string
    field :type, Ecto.Enum, values: @user_types

    many_to_many :roles, Role, join_through: "users_roles", on_replace: :delete

    has_many :permissions, through: [:roles, :permissions]

    timestamps()
  end

   schema "roles" do
    field :label, :string
    field :description, :string

    many_to_many(:permissions, Permission,
      join_through: "roles_permissions",
      join_keys: [role_id: :id, permission_key: :key],
      on_replace: :delete
    )

    many_to_many(:users, User, join_through: "users_roles")

    timestamps()
  end
   
  schema "permissions" do
    field :label, :string
    field :description, :string

    timestamps()
  end

When I create any test that utilizes insert(:user) I receive the following error:

    ** (Postgrex.Error) ERROR 23502 (not_null_violation) null value in column "inserted_at" of relation "roles_permissions" violates not-null constraint

         table: roles_permissions
         column: inserted_at

     Failing row contains (a63ceb35-3e7f-428d-8328-05e05a3fe6bb, approve_scripts, null, null).

The issue is simple to me. Basically, the underlying join table of roles_permissions doesn’t have an inserted_at or updated_at field. However, I don’t know what needs to be done to fix this. I would think that those fields would be inserted automatically so it doesn’t make sense why they would be null. Am I needing to create a roles_permissions file and table? Any help or guidance would be appreciated thank you.

EDIT

This is the current database schema in elixir code:

create table(:users) do
      add :emr_id, :citext
      add :username, :citext, null: false
      add :name_first, :text, null: false
      add :name_middle, :text
      add :name_last, :text, null: false
      add :type, :user_type, null: false
      timestamps()
    end

    create table("roles") do
      add :label, :text, null: false
      add :description, :text, null: false
      timestamps()
    end

    create table("permissions", primary_key: false) do
      add :key, :text, primary_key: true
      add :label, :text, null: false
      add :description, :text, null: false
      timestamps()
    end

    create table("roles_permissions", primary_key: false) do
      add :role_id, references(:roles), primary_key: true
      add :permission_key, references(:permissions, column: :key, type: :text), primary_key: true
      timestamps()
    end

    create table("users_roles", primary_key: false) do
      add :user_id, references(:users), primary_key: true
      add :role_id, references(:roles), primary_key: true
      timestamps()
    end
  end

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật