假设我有User和Organization的模型,以及两者之间的多到多关系。我现在想补充关于两者之间的关系的信息。例如,timestamps和position。如何进行此操作,以及如何查询这些信息?
我认为正确的方向是使用连接模式。在Ecto文档之后,应该是这样的:
defmodule UserOrganization do
use Ecto.Schema
@primary_key false
schema "users_organizations" do
field :position, :string # adding information on t
因此,根据Ecto文档中的示例,我有以下内容:
defmodule Post do
use Ecto.Schema
schema "posts" do
many_to_many :tags, Tag, join_through: "posts_tags"
end
end
和
defmodule Tag do
use Ecto.Schema
schema "tags" do
many_to_many :posts, Post, join_through: "posts_tags"
end
en
我是经济学人的新手。我在Ecto模式中定义了三个表,名为User、Role和UserRole。在UserRole中,我需要在关联用户表和角色表时更新附加字段(例如"status"),这将在UserRole表中生成一个条目。
// User Schema
schema "users" do
field :first_name, :string
field :last_name, :string
field :email, :string
many_to_many :roles, Role, join_through: "u
我正试图在Ecto 2中创建一个自我引用的many_to_many关系。我跟踪了的博客,并使它到目前为止的工作。但是,尝试使用Ecto.Changeset.put_assoc更新关联总是会导致错误。我不明白为什么。
这是设置:
首先,创建用户的迁移和联系人的关联表(每个用户都可以有多个联系人,其中也是用户):
# priv/repo/migrations/create_users_table.ex
defmodule MyApp.Repo.Migrations.CreateUsersTable do
use Ecto.Migration
def change do
cre
我有3个型号: user.ex schema "users" do
...
many_to_many(:acts, Act, join_through: UserAct)
end act.ex schema "acts" do
...
many_to_many(:users, User, join_through: UserAct)
end user_act.ex schema "users_acts" do
belongs_to :user, User
belongs_to
不确定Ecto为什么返回不正确的结果。我可能会把事情搞砸了
这是我的Ecto.Query
from u in User,
join: c in assoc(u, :companies),
join: r in assoc(c, :roles),
join: a in assoc(r, :assets),
where: u.id == ^2 and a.id == ^1,
group_by: [c.id, u.id, r.id],
select: {u.email, u.id, r.nam
我有这张桌子Chatactions
defmodule Module.Chats.Chataction do
use Ecto.Schema
import Ecto.Changeset
alias Module.Chats.ChatactionReadat
alias Module.Users.User
schema "chatactions" do
field :action, :string
...
many_to_many :readat, ChatactionReadat, join_through: "chat
与一起,我有两个模式之间的多对多关系-技术和作业,如下所示:
defmodule JobJawn.Listing.Job do
@moduledoc """
A job is a position for which a JobJawn user might want to apply. This is the
central model for the system - meaning it's the reason a user might want to
visit the system.
"""
可以使用连接值模式将其用于where子句吗?
filtered = [name: "show", name: "comedy"]
query = from s in Core.Serie, join: t in assoc(s, :tags), where: ^filtered
结果是下一个
#Ecto.Query<from s in Core.Serie, join: t in assoc(s, :tags),
where: s.name == ^"show" and s.name == ^"comedy">
我
我有以下模型(用于聊天室),在使用变更集时遇到问题:
defmodule Elemental.TxChat.Room do
use Elemental.TxChat.Web, :model
schema "rooms" do
field :name, :string
# Foreign key indicating which user created this room
# One user can create any number of rooms
belongs_to :created_by, Elemental.TxCha
问题:多对多表的FK字段仍然为空我有一个简单的多对多表来连接用户表和雇主表:
用户表:
schema "users" do
field :email, :string
field :first_name, :string
field :hashed_password, :string, redact: true
field :password, :string, virtual: true
field :password_confirmation, :string, virtual: true
field :is_active
I am learning Ecto and was trying out many to many relationship.
I am using MySQL DB,
模式是这样的
用户模式
@primary_key {:id, :binary_id, autogenerate: true}
schema "users" do
field(:name, :string, required: true)
field(:hashed_password, :string, required: true)
field(:password,
我在试用Ecoto2RC。
我的模特是:
schema "containers" do
field :name, :string
many_to_many :items, Test.Item, join_through: Test.ContainerItem, on_delete: :delete_all
timestamps
end
schema "items" do
field :content, :string
many_to_many :containers, Test.Container, join_through: Test
当涉及到在现有模型中添加一个新的关联时,我对Ecto感到相当困惑。假设我有一些讨论板,用户可以在其中发布一个帖子,我有以下模型:
schema "discussion_items" do
many_to_many :likes, XXX.Tag, join_through: "discussion_items_likes"
timestamps()
end
schema "likes" do
belongs_to :user, XXX.User
timestamps()
end
现在假设我有一个现
我正在开发一个多用户多室聊天应用程序,我的模型如下(为简单起见,应用程序模型被省略了):
defmodule Elemental.TxChat.User do
use Elemental.TxChat.Web, :model
schema "users" do
# The rooms the user is currenly logged into
many_to_many :rooms, Elemental.TxChat.Room, join_through: "rooms_users"
timestamps()
end
我正在开发一个聊天工具。我有很多房间,这些房间里有我的成员。用户也可以加入多个房间,因此这是一个多对多的关系。我想要一个房间,预加载它的成员,并为每个成员计算他有多少未读的消息。
下面是我在ecto中尝试完成的请求:
room_id = "general"
members_preload = from u in User,
join: ru in RoomUser, on: ru.user_id == u.id,
join: r in Room, on: r.id == ru.room_id,
我正试图确定一种正确的方法来验证Ecto 2中的多对多关系。我有一个需要多个成员的会话模型,用户可以成为许多对话的一部分,所以我已经建立了这样的模型:
# User Model
defmodule MyApp.User do
...
schema "users" do
....
many_to_many :conversations, Conversation, join_through: "conversations_users"
...
end
...
end
# Conversation Model
de
我是第一次使用Ecto (和Postgres),我有以下两个模式(都有些简化):
defmodule RailroadServer.Database.RailroadSystem do
@moduledoc """
Schema for an entire railroad system.
"""
use Ecto.Schema
import Ecto.Changeset
alias RailroadServer.Database
schema "railroad_systems" do
fi
我正在尝试插入两条相关的记录。当我尝试创建新用户时。我想首先在pools表中创建一条记录,并将其‘id’传递给users' table pool_id`。我正在尝试使用Ecto.Multi,但没有成功。如果您能给我指导或帮助,我将不胜感激!提前感谢!我从未使用过Ecto.Multi,因此,如果您能给我解释一下,我将不胜感激。结尾处的错误。 下面是我的用户模式: schema "users" do
field :email, :string
field :full_name, :string
field :password_digest, :
我正在尝试查询带有前缀的many_to_many关系,如下所示: Student
|> join(:left, [s], t in assoc(s, :teachers))
|> Repo.all(prefix: "my_prefix") 这将导致一个PostgreSQL查询: SELECT s0."id", s0."name", s0."inserted_at", s0."updated_at"
FROM "my_prefix"."students" AS s0
LE