我正在尝试完成一个测试,当且仅当用户具有“处方审批角色”角色并具有“approve_scripts”权限时,用户才能创建处方。为了完成测试,我使用 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
这是对应并映射到我的工厂的模式:
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
当我创建任何使用
insert(:user)
的测试时,我收到以下错误:
** (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).
这个问题对我来说很简单。基本上,
roles_permissions
的基础连接表没有 inserted_at
或 updated_at
字段。但是,我不知道需要做什么来解决这个问题。我认为这些字段会自动插入,所以它们为什么会是 null
是没有意义的。我需要创建 roles_permissions
文件和表格吗?任何帮助或指导将不胜感激,谢谢。
这是 Elixir 代码中当前的数据库模式:
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
您可以将其添加到工厂,以便它将随机日期时间添加到时间戳中:
timestamps =
DateTime.utc_now()
|> DateTime.add(-Enum.random(0..10_000), :second)
|> DateTime.truncate(:second)
%Permission{
key: "approve_scripts",
label: "Prescription Approval Permission",
description: "Permission to approve scripts"
inserted_at: timestamps,
updated_at: timestamps
}