# Help with Nullable Foreign Reference in Beam

**URL:** <https://discourse.haskell.org/t/help-with-nullable-foreign-reference-in-beam/8002>\
**Category:** Uncategorized\
**Created:** [November 4, 2023, 7:01pm UTC](https://discourse.haskell.org/t/help-with-nullable-foreign-reference-in-beam/8002 "2023-11-04T19:01:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/taylor/32/3147_2.png) [@taylor](https://discourse.haskell.org/u/taylor)\
**Post date:** [November 4, 2023, 7:01pm UTC](https://discourse.haskell.org/t/help-with-nullable-foreign-reference-in-beam/8002/1 "2023-11-04T19:01:31Z")

</div>

When you create a beam table it looks like this. From the documentation you do something that they call the ‘Identity Trick’ to get a constructor for the primary key:

```haskell
data RelayT f = Relay {
        _rid :: C f Int64
      , _uri :: C f Text
      } deriving (Generic, Beamable)
type Relay = RelayT
type RelayId = PrimaryKey RelayT Identity 
instance Table RelayT where 
      data PrimaryKey RelayT f = RelayId (C f Int64) deriving (Generic, Beamable)
      primaryKey = RelayId . _rid

```

Then you are able to create related tables like this:

```haskell
data EvT f = Ev {
        _eid :: C f Text 
      , _source :: PrimaryKey RelayT f
      } deriving (Generic, Beamable)
type Ev = EvT 
type EvId = PrimaryKey EvT Identity 
instance Table EvT where 
      data PrimaryKey EvT f = EvId (C f Text) deriving (Generic, Beamable)
      primaryKey = EvId . _eid

```

Then you are able to construct a value to insert into the database:

```haskell
e :: EvT Identity
e = Ev "example" (RelayId 1) 

```

From the docs (and the types work) it is possible to create a nullable foreign reference in your table:

```haskell
 , _source :: PrimaryKey RelayT (Nullable f) 

```

But I can’t figure out how to construct that value. Non foreign key nullables work quite simply with maybe but these permutation don’t work here.

- Just (RelayId 2)
- RelayId (Just 3)
- …

Anyone know how to construct a nullable foreign reference in beam?

[pushed a branch with the holes](https://github.com/AutonomousOrganization/futr2/tree/nullref)

I feel like it’s something I don’t understand about what this Identity Trick is actually doing so perhaps just trying to explain that to me will induce a lightbulb.

---

<div class="post-metadata">

**Author:** ![ocramz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/ocramz/32/3713_2.png) [@ocramz](https://discourse.haskell.org/u/ocramz)\
**Post date:** [November 5, 2023, 5:00am UTC](https://discourse.haskell.org/t/help-with-nullable-foreign-reference-in-beam/8002/2 "2023-11-05T05:00:34Z")

</div>

The “identity trick” is how higher-kinded types like `C f Int` where `f :: * -> *` simplify to the underlying type (`Int`)

TBH `beam` type sigs give me a headache but I have two concerns:

1. is it a good idea to have a nullable foreign key?
2. it seems you can only have nullable references if your Table type satisfies the `SqlJustable` constraint : [Database.Beam.Schema.Tables](https://hackage.haskell.org/package/beam-core-0.10.1.0/docs/Database-Beam-Schema-Tables.html#t:SqlJustable)

---

<div class="post-metadata">

**Author:** ![silky](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/silky/32/3555_2.png) [@silky](https://discourse.haskell.org/u/silky)\
**Post date:** [November 5, 2023, 4:23pm UTC](https://discourse.haskell.org/t/help-with-nullable-foreign-reference-in-beam/8002/3 "2023-11-05T16:23:18Z")

</div>

I was curious, so I looked into the beam-core library and the [tests](https://github.com/haskell-beam/beam/blob/master/beam-core/test/Database/Beam/Test/SQL.hs).

If you glance here - [https://github.com/haskell-beam/beam/blob/master/beam-core/test/Database/Beam/Test/Schema.hs#L124](https://github.com/haskell-beam/beam/blob/master/beam-core/test/Database/Beam/Test/Schema.hs#L124) - you can see that the `Department` type is a nullable foreign key; and you can see where they use it here - [https://github.com/haskell-beam/beam/blob/master/beam-core/test/Database/Beam/Test/SQL.hs#L840](https://github.com/haskell-beam/beam/blob/master/beam-core/test/Database/Beam/Test/SQL.hs#L840) - notably, the `Just` floats into the values of the foreign-key; so it seems to me it should just be `RelayId (Just 3)` as you suggested above.

Are you sure that doesn’t work?

---

<div class="post-metadata">

**Author:** ![taylor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/taylor/32/3147_2.png) [@taylor](https://discourse.haskell.org/u/taylor)\
**Post date:** [November 5, 2023, 6:38pm UTC](https://discourse.haskell.org/t/help-with-nullable-foreign-reference-in-beam/8002/4 "2023-11-05T18:38:50Z")

</div>

Oops, indeed it does
