Adding smart relationships while preserving the FK.
Observed behavior
Using addManyToOneRelation on a collection hides the foreign key from the .forestadmin-schema.json. This is problematic because we’re migrating from the old version on which we have multiple segments that depend on the foreign key.
Expected behavior
addManyToOneRelation should (optionally?) not remove the FK from the schema.
Context
Agent technology: nodejs
Agent (forest package) name & version: 1.40.1 with sequelize 6.37.3 and sequelize-typescript 2.1.6
Database type: MSSQL
Recent changes made on your end if any: Migrating from old agent.
The database field being CompanyId, I sacrifice CompId to the addManyToOneRelation overlords and preserve CompanyId.
It’s not a pretty solution but it does the trick.
It could be nice to be able to preserve the foreign key in the model. Thankfully, your migration guides include section about comparing schemas, which is how I could notice that I was missing the FK.
Sadly that did not work. Adding the importField after addManyToOneRelation does nothing to the schema.
To check whether I was messing up I did try using importField with a random name than the foreign key field and it works fine, so the issue is really on the foreign key field.
Indeed, the foreign key field is replaced with the relation, and cannot be remapped with another field. We can’t really fix this without reworking the whole system.
Your temporary workaround seems the right way to deals with your issue, at least until you migrate your segments to use the subfield “id” from the “company” field.