I expected to be able to add a column to a table within a schema by passing the schema through the options parameter, similar to createTable. E.g.
queryInterface.addColumn(
'myTable',
'test',
{
type: Sequelize.STRING,
},
{
schema: 'mySchema'
}
)
With a little digging, you have to pass an object to tableName
parameter, e.g.
{tableName: 'myTable', schema: 'mySchema'}
From the docs i have read it was not obvious how to create a column on a table in a schema, and i expected it to be the same syntax as createTable
.
Is this expected?
Or should i create a pull request for addColumn
to have the same syntax of passing a schema as createTable
?
I figured this out. addColumn can use the same syntax show in #2677.
return queryInterface.addColumn({
tableName: 'SomeTableNameHere',
schema: 'mySchema'
}, 'columnNameHere', {
type: Sequelize.DATE,
allowNull: true,
});
The removeColumn works the same:
return queryInterface.removeColumn({
tableName: 'SomeTableNameHere',
schema: 'mySchema'
}, 'columnNameHere');
All methods (should) support { tableName, schema }
, docs PR wlecome
Hi Guys,
@janmeier
This is not working for me in Mysql (MariaDB)
sequelize-cli has a database config file with three environments, it selects the initial database from there, call it DatabaseA
when I do something like
queryInterface.addColumn({
tableName: 'table1',
schema: 'DatabaseB'
}, 'column1');
the resulting query shown in the error log is
Alter table DatabaseB.table1 add column column1
but the error is
code: 'ER_NO_SUCH_TABLE',
errno: 1146,
sqlState: '42S02'
with the Message : Table DatabaseA.DatabaseB.table1 does not exist
Somehow, it seems correct because when you log into Mysql Cli, I need to have a database selected, and having database A selected I cant alter a table in Database B.
Please help if Im not on the right track with this.
Most helpful comment
I figured this out. addColumn can use the same syntax show in #2677.
The removeColumn works the same: