# Many to many pivot with additional variables

**URL:** <https://forums.kodeco.com/t/many-to-many-pivot-with-additional-variables/77877>\
**Category:** Server Side Swift with Vapor\
**Created:** [August 1, 2019, 12:00am UTC](https://forums.kodeco.com/t/many-to-many-pivot-with-additional-variables/77877 "2019-08-01T00:00:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramelan](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ramelan/32/110937_2.png) [@ramelan](https://forums.kodeco.com/u/ramelan)\
**Post date:** [August 1, 2019, 12:00am UTC](https://forums.kodeco.com/t/many-to-many-pivot-with-additional-variables/77877/1 "2019-08-01T00:00:36Z")

</div>

Hi,

I’m trying to create a many to many relationship with additional variables but can not find any examples.

I have User and Group objects and want to create if a user has either view or write privileges for groups.

here is my pivot class definition:

> import FluentPostgreSQL  
> import Foundation
> 
> ```
> final class UserGroupPivot: PostgreSQLUUIDPivot {
> var id: UUID?
> var canRead: Bool
> var canWrite: Bool
> 
> var userID: User.ID
> var groupID: Group.ID
> 
> typealias Left = User
> typealias Right = Group
> 
> static let leftIDKey: LeftIDKey = \.userID
> static let rightIDKey: RightIDKey = \.groupID
> 
> init (_ user: User, _ group: Group) throws {
> self .userID = try user.requireID()
> self .groupID = try group.requireID()
> 
> self .canRead = false
> self .canWrite = false
> }
> 
> init (_ user: User, _ group: Group, canRead: Bool, canWrite: Bool) throws {
> self .userID = try user.requireID()
> self .groupID = try group.requireID()
> 
> self .canRead = canRead
> self .canWrite = canWrite
> }
> 
> }
> 
> extension UserGroupPivot: Migration {}
> extension UserGroupPivot: ModifiablePivot {}
> 
> ```

I can see in the tables that the extra variables are created, but I don’t know how to populate these data.

How can I do a CRUD operation also how to know if a user can write to a group.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![ramelan](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ramelan/32/110937_2.png) [@ramelan](https://forums.kodeco.com/u/ramelan)\
**Post date:** [August 13, 2019, 10:34am UTC](https://forums.kodeco.com/t/many-to-many-pivot-with-additional-variables/77877/2 "2019-08-13T10:34:07Z")

</div>

Will we be able to do this in Vapor 4?

thanks

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [August 15, 2019, 11:22am UTC](https://forums.kodeco.com/t/many-to-many-pivot-with-additional-variables/77877/3 "2019-08-15T11:22:17Z")

</div>

@ramelan hey apologies for the delay! I’m not sure what the state of Vapor 4 Fluent is but it should definitely be possible. However you can do it in Vapor 3 as well, you just may need to write some queries yourself.

So to start with you can’t use a `ModifiablePivot`, only a `Pivot` - that means you can’t use the `attach` function but you can create the pivots manually and just save them (since they’re just models). Since you don’t need `ModifiablePivot` you can remove the `init (_ user: User, _ group: Group)` initialiser and that gives you the option to create the pivots however you like. Does that make sense?

For querying, things get a bit more complicated. It _may_ be possible to specify more filter options `user.groups.filter(...).query(on: req)` but I’m not sure that will work. If you look into the implementation of `siblings()` you can just copy that and add your extra filter options there. Hope that helps!

---

<div class="post-metadata">

**Author:** ![ramelan](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ramelan/32/110937_2.png) [@ramelan](https://forums.kodeco.com/u/ramelan)\
**Post date:** [August 19, 2019, 1:08am UTC](https://forums.kodeco.com/t/many-to-many-pivot-with-additional-variables/77877/4 "2019-08-19T01:08:20Z")

</div>

Hi @0xtim,

Thank you for your help. Apologize for the late reply too as I’m traveling now.

I’ll try to implement your suggestion and let you know how it goes.

Thanks again
