Model generation

Feature(s) impacted

Our data model has evolved this led us to regenerate the model

Observed behavior

The new files have a lot of difference with our old ones, in particular on the associations:

All belongsTo disappeared.

Expected behavior

Is this a change in how FA works? or a problem in the generation of our model?

Failure Logs

Context

Development environment

  • Project name: Sergic
  • Team name: …
  • Environment name: development
  • Agent type & version: …
  • Recent changes made on your end if any: …

Hi @celloudara !
How did you generate the new files ?

To generate the models I initialized a new FA project

Can you try to follow this part of the documentation instead ?

Hi @Nicolas_Sailly

I started by using this documentation but I have the error below however the application starts correctly

forest schema:update
× Connecting to your database(s)
× An unexpected error occurred. Please reach out for help in our Developers community (https://community.forestadmin.com/) or create a Github issue with following error: Dialect needs to be explicitly supplied as of v4.0.0

Mmmh we have another thread with the same error here.
Otherwise, it seems this is an error that can appear when the NODE_ENV environment variable isn’t set as shown here.

Please let me now if one of those helps to solve your issue :slight_smile:

H @Nicolas_Sailly

I managed to run the forest schema:update command, I was missing the .env file, we use .env.development, .env.stasing files, …

But it does not update the file if it already exists in models, so I deleted all the files in models to recreate them and the relationships have all disappeared is this normal?

If the relationships still exist in your database they should not disappear :thinking:
Are they written in your .forestadmin-schema.json ?

Hi @Nicolas_Sailly

Yes they are in the .forestadmin-schema.json file
Below is an example of behavior:

Table with relationship:

CREATE TABLE parcel_contracts (
	id bigserial NOT NULL,
	parcel_id int4 NULL,
	contract_id int4 NULL,
	created_at timestamp NOT NULL,
	updated_at timestamp NOT NULL,
	CONSTRAINT parcel_contracts_pkey PRIMARY KEY (id),
	CONSTRAINT fk_rails_43e35cb79b FOREIGN KEY (contract_id) REFERENCES public.contracts(id),
	CONSTRAINT fk_rails_d0da44fdd0 FOREIGN KEY (parcel_id) REFERENCES public.parcels(id)
);

Front model file:

// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  // This section contains the fields of your model, mapped to your table's columns.
  // Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const ParcelContracts = sequelize.define('parcelContracts', {
    createdAt: {
      type: DataTypes.DATE,
    },
    updatedAt: {
      type: DataTypes.DATE,
    },
  }, {
    tableName: 'parcel_contracts',
    underscored: true,
    schema: process.env.DATABASE_SCHEMA,
  });

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  ParcelContracts.associate = (models) => {
    ParcelContracts.belongsTo(models.contracts, {
      foreignKey: {
        name: 'contractIdKey',
        field: 'contract_id',
      },
      as: 'contract',
    });
    ParcelContracts.belongsTo(models.parcels, {
      foreignKey: {
        name: 'parcelIdKey',
        field: 'parcel_id',
      },
      as: 'parcel',
    });
  };

  return ParcelContracts;
};

New file:

// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  // This section contains the fields of your model, mapped to your table's columns.
  // Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const ParcelContracts = sequelize.define('parcelContracts', {
    parcelId: {
      type: DataTypes.INTEGER,
    },
    contractId: {
      type: DataTypes.INTEGER,
    },
    createdAt: {
      type: DataTypes.DATE,
    },
    updatedAt: {
      type: DataTypes.DATE,
    },
  }, {
    tableName: 'parcel_contracts',
    underscored: true,
    schema: process.env.DATABASE_SCHEMA,
  });

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  ParcelContracts.associate = (models) => {
  };

  return ParcelContracts;
};

Is it normal?

I’ll let @romaing help you outm he has more expertise than me on the subject :wink:

Hi @Nicolas_Sailly @romaing

Is it possible that there is an evolution to no longer systematically generate related dates and leave this generation to the control of the devs?

Not that I know.

I just tested on a database on my machine with the latest forest-cli version, and everything seems to be working (I still get all the relations).

I tried reviewing recent changes in the commit history, but don’t see anything related to your issue (missing relations).

forest-cli relies on the database having foreign key constraints to discover relations but you seem to have those.

I’m thinking maybe it can be an issue if you are using multiple postgres schemas?
But you don’t seem to be doing that (in the SQL sample I see references to the public schema, which is the default one on postgres).