Lachezar Raychev
Lachezar Raychev

Reputation: 2113

Sequelize, set column as foreign key to a table from another schema

I am trying to create a table:

            queryInterface.createTable('MyTable', {
                id: {
                    type: Sequelize.INTEGER,
                    primaryKey: true,
                    autoIncrement: true
                },
               SomeTableId: {
                    type: Sequelize.INTEGER,
                    references: { model: 'static.SomeTable', key: 'id'},
                    allowNull: false
                },

            }, t);

The problem is that this error is thrown, when I run the migration :

'Unhandled rejection SequelizeDatabaseError: relation "static.SomeTable" does not exist'

So, basically, the question is:

When I am creating a table in the 'public' schema, how can I specify a foreign key column in that table, that references a table in the 'static' schema.

Upvotes: 6

Views: 3084

Answers (1)

Lachezar Raychev
Lachezar Raychev

Reputation: 2113

Ok, the right syntax is:

 queryInterface.createTable('MyTable', {
                id: {
                    type: Sequelize.INTEGER,
                    primaryKey: true,
                    autoIncrement: true
                },
               SomeTableId: {
                    type: Sequelize.INTEGER,
                    references: { 
                        model: {
                            tableName: 'SomeTable', 
                            schema: 'static'
                        }
                        key: 'id'
                    },
                    allowNull: false
                },

            }, t);

And we are done, problem solved :)

Upvotes: 16

Related Questions