Postgres Table(s)
I have a user resource and I need to assign a user to one_on_one resource. The one_on_one resource has two arguments user_1 and user_2. I am at a loss on how I should define the relationship. I know how I want the table design to be:
Where user_id_1 can't be the same as user_id_2. Anyone else struggle with this part of Ecto?
20 Replies
So when you say
one_on_one
you mean like a join resource that a many_to_many
might have, but there is only one for any given user? Or is there one for any given user combination?one_on_one_meeting
is likely a better name.
many_to_many
is what I am thinking.
This is what I came up with. No relationships here or in the user resource.Why not use relationships?
manage_relationship
won't work without a relationshipI want to use relationships. Just going from table schema -> ecto schema has always been an issue for me. I haven't done them enough to have them committed to memory.
I was trying to use the helpdesk example for my example, but it didn't fit 100% since I have more than one id vs just a single id.
So this is interesting. I setup the
belongs_to
on OneToOne resource. I setup the has_many
on the user resource and set destination_attribute(:user_1_id)
and it looks like ash figured out that :user_1_id
are foreign keys.
But what is weird is if I create an one_on_one
resource either with ids set, nothing gets set for both ids, and the same thing happens if I try to update an existing one_on_one
resource.
OH! I needed to do Ash.Changeset.manage_relationship/4
and also add on_lookup: :relate
!!!! Now I can see the IDs as part of OneOnOne resource!
zach: I hope my comments are enough that you can make sense of it. LOLYou can do that 🙂 You can also do this:
That will make your actions accept
user_id
without all of the manage_relationship
stuffOH!
Hopefully I can pass the struct too.
🤔 I don't believe you can
What is the best way to do database transactions?
Oh, so then it would be better to do
manage_relationship
right?🤔 I'd probably just do it with the ids personally
Ok, it is hard to use the wrong uuid.
you could do something like this:
If you wanted to accept a
%User{}
and not a user.id
Thanks! you were right though, I couldn't use
%User{}
For transactions, it depends on what exactly you're trying to do.
1. if you need to explicitly start a transaction, you can do
YourApp.YouRepo.transaction(fn -> ...do your work here end)
2. there is a transaction lifecycle for all create/update/destroy actions that use ash_postgres. By default they all start a transaction to do there work in unless you specify transaction? false
in the action. You have hooks to add behavior for before_transaction -> before_action -> <actual action at data layer happens here> -> after_action -> after_transaction
I want to commit 3 resources since I don't want something dangling if one of the 3 resources aren't committed.
For example:
You can add multiple hooks for each part of the lifecycle, that is just an example change that uses each hook once
phew, that is a lot to digest. 😄
So, if you do your work inside of
before_action
or after_action
hooks, then it will all automatically happen in a database transactionYou can read more about the action lifecycle here 🙂 https://ash-hq.org/docs/guides/ash/latest/topics/actions#action-lifecycle
I have been all over the site tonight. LOL I have seen this page at least once.
I can open a new support if you want. But I want to have I think a calculation where I sum one query, sum another query and then subtract sum_one minus sum_two. I would normally do this separately in a context with 2 functions. One that sums and then another than calls the sum function but then subtracts the two sums.
yes, please open another support issue if you don't mind 😄