sequalize associations for make relation between tables of multiple schema

Viewed 17

I am working with a project where I am using sequalize and postgres as database. So, in the project I am creating schema dynamically when a new user register in the website.

Now I have a table named as user_credentials. This table is located on the custom private schema which is created by the user. When a user register in the website all of his data will be stored in User table, which is located in Public schenma.

Now, I want to make a oneToOne relation between Public schema users table and the private schema User_credentials table.

How I can do that in sequalize?

 User.hasOne(UserCredential(schema) , {schema:schema });
 
UserCredential(schema).belongsTo(User ,  { schema:'public' });

here as you can see there is parametre called schema. This is dynamic coming from user. And for the User table we are not passing the schema name because we are using default public schema for that.

I need to create relation between this two tables which are located in different schema. please help me. Thank in advance.

1 Answers

All you need to do is to indicate a correct schema option in each model definition right next to a table name (associations don't have such option).

See schema and tableName options here

Related