# One-to-many relation data implementation

**URL:** <https://forum.fuel.network/t/one-to-many-relation-data-implementation/2884>\
**Category:** Sway\
**Tags:** faqs\
**Created:** [July 13, 2023, 8:15am UTC](https://forum.fuel.network/t/one-to-many-relation-data-implementation/2884 "2023-07-13T08:15:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![timotejGerzelj](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/timotejgerzelj/32/1553_2.png) [@timotejGerzelj](https://forum.fuel.network/u/timotejGerzelj)\
**Post date:** [July 13, 2023, 8:15am UTC](https://forum.fuel.network/t/one-to-many-relation-data-implementation/2884/1 "2023-07-13T08:15:01Z")

</div>

I would like to implement a one-to-many data structure but from what I read a classic key to array solution is not possible, I have also thought about having a nested StorageVec which however I am not sure is the best way. Would anyoe know a better workaround?

---

<div class="post-metadata">

**Author:** ![calldelegation](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/calldelegation/32/1484_2.png) [@calldelegation](https://forum.fuel.network/u/calldelegation)\
**Post date:** [July 13, 2023, 2:57pm UTC](https://forum.fuel.network/t/one-to-many-relation-data-implementation/2884/2 "2023-07-13T14:57:53Z")

</div>

Hi again @timotejGerzelj this should be possible try this! Additionally take a look at this forum post which I think might be helpful for you. [Is there a way to assign StorageVec inside a struct and then the struct will be used in maps](https://forum.fuel.network/t/is-there-a-way-to-assign-storagevec-inside-a-struct-and-then-the-struct-will-be-used-in-maps/2833).

```auto

use std::storage::storage_vec::*;

storage {
    map: StorageMap<Address, StorageVec<u64>> = StorageMap {},
}

...

```

toolchain version I’m running

```auto
active toolchain
-----------------
latest-aarch64-apple-darwin (default)
  forc : 0.42.0
    - forc-client
      - forc-deploy : 0.42.0
      - forc-run : 0.42.0
    - forc-doc : 0.42.0
    - forc-explore : 0.28.1
    - forc-fmt : 0.42.0
    - forc-index : 0.17.3
    - forc-lsp : 0.42.0
    - forc-tx : 0.42.0
    - forc-wallet : 0.2.3
  fuel-core : 0.18.3
  fuel-indexer : 0.17.3

fuels versions
---------------
forc : 0.43
forc-wallet : 0.43.0

```

---

<div class="post-metadata">

**Author:** ![timotejGerzelj](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/timotejgerzelj/32/1553_2.png) [@timotejGerzelj](https://forum.fuel.network/u/timotejGerzelj)\
**Post date:** [July 13, 2023, 4:24pm UTC](https://forum.fuel.network/t/one-to-many-relation-data-implementation/2884/3 "2023-07-13T16:24:48Z")

</div>

Hi @calldelegation thanks again for a response! The reason I thought this type of data-structure is not possible is because of this [post here](https://forum.fuel.network/t/no-method-named-push-found-for-type-storagevec-u64/2838) since I was receiving the same error. Could you show me how to update the StorageVec in StorageMap in this case if you dont mind?

---

<div class="post-metadata">

**Author:** ![calldelegation](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/calldelegation/32/1484_2.png) [@calldelegation](https://forum.fuel.network/u/calldelegation)\
**Post date:** [July 13, 2023, 5:45pm UTC](https://forum.fuel.network/t/one-to-many-relation-data-implementation/2884/4 "2023-07-13T17:45:43Z")

</div>

Yes ofcourse! Assuming you’re on the latest version of the toolchain try something like this:

```auto
contract;

use std::storage::storage_vec::*;

storage {
    map: StorageMap<u64, StorageVec<u64>> = StorageMap {},
}

abi MyContract {
    #[storage(read, write)]
    fn insert_function(id: u64);

    #[storage(read, write)]
    fn push_function(id: u64);
}

impl MyContract for Contract {
    #[storage(read, write)]
    fn insert_function(id: u64) {
        let nums: StorageVec<u64> = StorageVec {};
        storage.map.insert(id, nums);
    }

    #[storage(read, write)]
    fn push_function(id: u64) {
        storage.map.get(id).push(100); 
        // Manipulating vecs inside a mapping (.pop(), .remove() etc.)
    }
}

```

Theres a ton of helper functions other than `.push` for you to manipulate the StorageVecs [here](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/storage/storage_vec.sw)

---

<div class="post-metadata">

**Author:** ![timotejGerzelj](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/timotejgerzelj/32/1553_2.png) [@timotejGerzelj](https://forum.fuel.network/u/timotejGerzelj)\
**Post date:** [July 14, 2023, 8:59am UTC](https://forum.fuel.network/t/one-to-many-relation-data-implementation/2884/5 "2023-07-14T08:59:21Z")

</div>

@calldelegation once again thank you! You really are helping me a lot!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/57d5a345cc15a64b636e0d56e042857f8a0e80b1.png) [@system](https://forum.fuel.network/u/system)\
**Post date:** [August 4, 2023, 5:00am UTC](https://forum.fuel.network/t/one-to-many-relation-data-implementation/2884/6 "2023-08-04T05:00:00Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
