#Why can I not set a Foreign Key?

3 messages · Page 1 of 1 (latest)

zenith heron
#

Hello Everyone,

I have tried everything I can think of to do this, and read the relationship docs a few times.

I want to create a one-to-many relationship between one Customer and many Contacts i.e. individuals working at that Customer.

Here's what I think I'm doing:

  1. Set up a Customers Collection
  2. Set up a Contacts Collection
  3. Enter Customers Data Model
    4.0 Add field "one-to-many"
    4.1 Give field a unique name of "contact_id"
    4.2 Select related collection "Contacts"
    ⛔️ 4.3 Select a foreign key - no option

I expect the foreign key to link to the Contact somehow but there's some knowledge I'm missing.

What am I doing wrong?

zenith heron
#

Having gone through the Relationships documentation like a tutorial using the models provided, the missing knowledge became more apparent to me.

The modelling process regardless of one to many or many to one, is executed on the "many" side of the relationship when modelling data from inside Directus. If you are capturing models from a pre-existing database I speculate these features might work differently but I don't know for certain.

Because I am using Directus as an interface for a back office tool, instead of modelling data for a front-end, I want to make things work a bit differently when I model that way. And I'm not sure the documentation is set up for that way of thinking (and the fact that I'm a fairly inexperienced data modeller).

Anyway, my learning is that I should configure on the Contacts side (where many Contacts relate to one Customer) of the relationship, not Customers.

I can display one Company against many Contacts with the one-to-many option (note this is done from inside the Contacts model). If building a front-end via the API this would work fine. However, I want to provide more editing functionality in the UI to someone working on a Customer inside Directus (using it as a backoffice tool). That means I have to add a many-to-one relation in Contacts, and Directus gives me the option to add an alias inside the Customers collection and adds a foreign_key inside Contacts for me automatically.

Hope this helps in case someone else comes up against this.

zenith heron
#

Nope - ignore the above.

I am now lost again, I had it working and now it doesn't