Complex resource generation?

Hey guys, I'm getting a bit of chicken & egg problem when I try to create my resources using generators. As most of them are somehow related to one another, I get all sorts of compile time errors, no matter which ones I try to run first. How do you handle these situations yourselves? Is it at all possible to build my app using generators, or do I have to manually write most of them? For example, a Subtitle belongs to a Video, and a Video has many Subtitles. Whenever I try to crate one, the othe must be already present, otherwise I get an error. Maybe I'm doing something wrong?
mix ash.gen.resource Susflix.Media.Video --uuid-primary-key id --relationship "has_many:subtitles:Susflix.Media.Subtitle"
mix ash.gen.resource Susflix.Media.Subtitle --uuid-primary-key id --relationship "belongs_to:video:Susflix.Media.Video"
mix ash.gen.resource Susflix.Media.Video --uuid-primary-key id --relationship "has_many:subtitles:Susflix.Media.Subtitle"
mix ash.gen.resource Susflix.Media.Subtitle --uuid-primary-key id --relationship "belongs_to:video:Susflix.Media.Video"
Compiling 107 files (.ex)
warning: invalid association `subtitles` in schema Susflix.Media.Video: associated schema Susflix.Media.Subtitle does not exist
└─ lib/susflix/media/video.ex: Susflix.Media.Video (module)

** (EXIT from #PID<0.95.0>) an exception was raised:
** (RuntimeError) Relationship Susflix.Media.Video.subtitles has an invalid destination: Susflix.Media.Subtitle.

lib/susflix/media.ex:1: Susflix.Media.__verify_spark_dsl__/1
(elixir 1.18.4) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(elixir 1.18.4) lib/module/parallel_checker.ex:244: Module.ParallelChecker.check_module/3
(elixir 1.18.4) lib/module/parallel_checker.ex:90: anonymous fn/7 in Module.ParallelChecker.inner_spawn/6
Compiling 107 files (.ex)
warning: invalid association `subtitles` in schema Susflix.Media.Video: associated schema Susflix.Media.Subtitle does not exist
└─ lib/susflix/media/video.ex: Susflix.Media.Video (module)

** (EXIT from #PID<0.95.0>) an exception was raised:
** (RuntimeError) Relationship Susflix.Media.Video.subtitles has an invalid destination: Susflix.Media.Subtitle.

lib/susflix/media.ex:1: Susflix.Media.__verify_spark_dsl__/1
(elixir 1.18.4) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(elixir 1.18.4) lib/module/parallel_checker.ex:244: Module.ParallelChecker.check_module/3
(elixir 1.18.4) lib/module/parallel_checker.ex:90: anonymous fn/7 in Module.ParallelChecker.inner_spawn/6
3 Replies
Gonzalo Muñoz
Gonzalo MuñozOP3mo ago
If I try to do it the other way around (subtitle first), I get this:
Compiling 2 files (.ex)
** (EXIT from #PID<0.95.0>) an exception was raised:
** (RuntimeError) Error when compiling `Susflix.Media.Subtitle`: Resource `Susflix.Media.Video` in relationship `:video` is not accepted by domain `Susflix.Media`. Please do one of the following

1. add the resource to the domain `Susflix.Media`
2. configure a different domain

lib/susflix/media.ex:1: Susflix.Media.__verify_spark_dsl__/1
(elixir 1.18.4) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(elixir 1.18.4) lib/module/parallel_checker.ex:244: Module.ParallelChecker.check_module/3
(elixir 1.18.4) lib/module/parallel_checker.ex:90: anonymous fn/7 in Module.ParallelChecker.inner_spawn/6
Compiling 2 files (.ex)
** (EXIT from #PID<0.95.0>) an exception was raised:
** (RuntimeError) Error when compiling `Susflix.Media.Subtitle`: Resource `Susflix.Media.Video` in relationship `:video` is not accepted by domain `Susflix.Media`. Please do one of the following

1. add the resource to the domain `Susflix.Media`
2. configure a different domain

lib/susflix/media.ex:1: Susflix.Media.__verify_spark_dsl__/1
(elixir 1.18.4) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(elixir 1.18.4) lib/module/parallel_checker.ex:244: Module.ParallelChecker.check_module/3
(elixir 1.18.4) lib/module/parallel_checker.ex:90: anonymous fn/7 in Module.ParallelChecker.inner_spawn/6
Is there maybe a way of generating everything first, and then compiling all together?
ZachDaniel
ZachDaniel3mo ago
Ohhh, I see your issue You can generate w/o the relationships first I thought you meant test data generation In the latest version, the generators will patch so you can add relationships in by running again w/ just the relationships
Gonzalo Muñoz
Gonzalo MuñozOP3mo ago
Then I run again the same ash.gen.resource tool to add the relationships? oh, ok thanks, I will try that

Did you find this page helpful?