yesodweb / persistent

Persistence interface for Haskell allowing multiple storage methods.
MIT License
467 stars 296 forks source link

Mutual foreign keys #517

Closed pseudonom closed 4 years ago

pseudonom commented 8 years ago

The presence of deferred constraints in postgres means it's a little sensible to have two tables, each with foreign keys pointing at each other. Unfortunately, it's hard to work with in persistent. I'd like to be able to do something like:

share [|mkPersist sqlSettings, mkMigrate "migrateAll"|] [persistLowerCase|
Foo
  barId BarId
Bar
  fooId FooId                          
|]

Because of the fact that declaration order matters in TH, the mutual recursion here doesn't work out. There's an ugly workaround where you declare a type like BarId' up front and have Foo use that. Is there a better way for persistent to support this scenario? Thanks.

snoyberg commented 8 years ago

Other alternatives:

There are no plans currently to implement a better solution in persistent, but I'd certainly be interested in a PR.

gregwebs commented 8 years ago

I would like to support groundhog's declaration style as an option

parsonsmatt commented 4 years ago

This works now