ForestAdmin / lumber

Install Forest Admin in minutes.
https://www.forestadmin.com
MIT License
2.08k stars 106 forks source link

fix: model generation inconsistencies #445

Closed closingin closed 4 years ago

closingin commented 4 years ago

Fixes #4embh7.

This PR contains to bug fixes in order to correctly handle snake_cased tables that only contain foreign keys.

The issue came from two different bugs in the code :

The consequence was that in the schema building step, after having generated your project, Sequelize couldn't find the snake_cased model in the through property, and as such created it on its own (which is its normal behaviour) with default values (such as createdAt and updatedAt).

From the documentation :

Movie.belongsToMany(Actor, { through: 'ActorMovies' });
Actor.belongsToMany(Movie, { through: 'ActorMovies' });

Since a string was given in the through option of the belongsToMany call, Sequelize will automatically create the ActorMovies model which will act as the junction model. For example, in PostgreSQL:

CREATE TABLE IF NOT EXISTS "ActorMovies" (
 "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
 "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
 "MovieId" INTEGER REFERENCES "Movies" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
 "ActorId" INTEGER REFERENCES "Actors" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
 PRIMARY KEY ("MovieId","ActorId")
);

Pull Request checklist:

arnaudbesnier commented 4 years ago

Task linked: CU-4embh7 P1 - Schema Generation - Table with underscores generated twice and with timestamps fields

closingin commented 4 years ago

I'm not sure it's related but tasks look to be displayed in a bad way. Could you try it on your end?

@larcin Pulling forestadmin-server made it work as it used to, it's ok for me!

forest-bot commented 4 years ago

:tada: This PR is included in version 3.6.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: