You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
changeColumn does not remove NOT NULL contraint #9635
If this doesn't work, I'll look into fixing this behaviour.
Reacted by Igor Leahu, Lasha Tsintsabadze, Widada, Rohan R, gmeneguz, Den Rupp, mieszko4, Giácomo Quinalia, Felix Dolderer, olivia johansen and 12 more
Reacted by Cristian Ebbens, Widada, Ihor Romanchuk, Lucas Rassilan Vilanova, Giácomo Quinalia, Hallex Abreu , David Axelrod and José Algorta
Reacted by Hallex Abreu , David Axelrod, Rafael Costa and José Algorta
Removing references from the changeColumn parameters worked - it changed allowNull to true without removing the foreign key. I assume that if I wanted to modify the constraint I'd use a combination of QueryInterface.removeConstraint and QueryInterface.addConstraint?
I'm using Sequelize 4.38.0.
Reacted by Thiago Silva Ferreira, Sebastian Campos, Christian Giordano, Samuel Lima, Rohan R, Kalpa IT Enterprises, gmeneguz, Alexei Paksevatkin, rsys-purnima-srivastava, Paweł and 12 more
Reacted by Mariano Fliri, Christian Farias Aguila, Bhuvaneshwarn R, Nate Dube, Thiago Silva Ferreira, Haru, Oula Antere, SpiritusDeos, Adams Kariuki Dennis, Alex and 24 more
Reacted by Haru, Oula Antere, SpiritusDeos, Adams Kariuki Dennis, alexander garza, mmalloc, mb8z, Roger Ramirez, Nwakasi Stephen Ifeanyi, bilal-zahid-invozone and 3 more
Reacted by Kostiantyn Kostiuk, Chuya Guo, Toma Zelić, Gideon Gitau, yasser, Alex, alexander garza, Pedro Guzmán, djustclean, George Kvaratskhelia and 15 more
Reacted by Hassam Saeed, LakmalHarshana, Bianca Caetano, Ayelegun Kayode Michael and Yasin
It's good to mention that using changeColumn and setting allowNull: falseDOES NOT remove the foreign key to the table, but it does remove the allowNull constraint. If you want to remove the foreign key, you have to use queryInterface.removeConstraint.
Reacted by Nate Dube, Allan, patryk-sienkiewicz-bi, Erkhembayar Gantulga, utkarsh-qwikskills, Isaac Olawale, eklenin, Niko and Vyacheslav Karpovych
I've had the same issues. I was adding a column to a MySQL table with a foreign key. So first adding the table allowing null(default=null), then running a migration step to populate the new column and finally a migrationstep to set allownull=false....
But it isn't working 100% as expected...it runs fine...but the result is that the column is still nullable after the last step....
I think it's because the "nullable property" is a really a property of the constraint of the foreign key and not of the column itself somehow... or at least somehow bound to it... if i try the same operation(set nullable=false) in MySQL workbench GUI and view the automatic generated SQL it seems to drop all contraints first, then apply the column change and then re-add the constraints...
I think the lesson for me is to create the column WITHOUT the reference first(=no constraints)....then fill the column with the correct id's...and THEN apply the foreign key as the last step with allow-null=false...
I'm doing a migration that adds a new column, update the information, and set the column to not nullable.
// This does notwork
await queryInterface.changeColumn('Documents', 'documentCategoryId', {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'DocumentCategories',
key: 'id'
}
})
// At this point, the column is nullable, but the reference is added
But when splitting this, it seems to work.
// This work using 2 changes instead
await queryInterface.changeColumn('Documents', 'documentCategoryId', {
type: Sequelize.INTEGER,
allowNull: false
})
await queryInterface.changeColumn('Documents', 'documentCategoryId', {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'DocumentCategories',
key: 'id'
}
})
// At this point, the first one changed the column to being not nullable + adds the reference
Activity
mirkojotic commentedon Jul 19, 2018
@sigod Have you tried excluding
referencefromchangeColumnstatement?like so:
If this doesn't work, I'll look into fixing this behaviour.
sigod commentedon Jul 20, 2018
If I remember correctly it then removes the foreign key.
nsfm commentedon Sep 17, 2018
I'm also experiencing this issue:
The migration runs successfully, but there's no apparent change.
I'll try without the references and see what happens.
nsfm commentedon Sep 17, 2018
Removing
referencesfrom the changeColumn parameters worked - it changed allowNull to true without removing the foreign key. I assume that if I wanted to modify the constraint I'd use a combination of QueryInterface.removeConstraint and QueryInterface.addConstraint?I'm using Sequelize 4.38.0.
srkimir commentedon Nov 24, 2018
As @nsfm pointed out, following is working for me as well
Sequelize CLI [Node: 8.12.0, CLI: 4.1.1, ORM: 4.38.0]
BhuvaneshwaranR commentedon Dec 31, 2018
@srkimir Thanks. The aforementioned code is working.
waldyrious commentedon Mar 21, 2019
The lack of the
typeproperty was the issue in my case:doesn't work, but with
up(queryInterface, Sequelize) { return queryInterface.changeColumn(tableName, columnName, { + type: Sequelize.STRING, allowNull: true }) }it works.
LeoDoldan7 commentedon May 24, 2019
It's good to mention that using
changeColumnand settingallowNull: falseDOES NOT remove the foreign key to the table, but it does remove the allowNull constraint. If you want to remove the foreign key, you have to usequeryInterface.removeConstraint.steveify1 commentedon Oct 15, 2020
Thanks @srkimir and @waldyrious
vtolenti89 commentedon Oct 30, 2020
I am also facing the same problem, but the above solutions did not work in my case. Here is my migration file:
Any ideas what is mising?
ThomasEg commentedon Dec 3, 2020
I've had the same issues. I was adding a column to a MySQL table with a foreign key. So first adding the table allowing null(default=null), then running a migration step to populate the new column and finally a migrationstep to set allownull=false....
But it isn't working 100% as expected...it runs fine...but the result is that the column is still nullable after the last step....
I think it's because the "nullable property" is a really a property of the constraint of the foreign key and not of the column itself somehow... or at least somehow bound to it... if i try the same operation(set nullable=false) in MySQL workbench GUI and view the automatic generated SQL it seems to drop all contraints first, then apply the column change and then re-add the constraints...
I think the lesson for me is to create the column WITHOUT the reference first(=no constraints)....then fill the column with the correct id's...and THEN apply the foreign key as the last step with allow-null=false...
ThomasEg commentedon Dec 3, 2020
Actually, i might think this is a bug...sortof...
Running this does NOT make the column nullable, but it completes just fine and gives no errors:
If i try the "same" thing using plain SQL:
...i'll get an error:
Shouldn't I get the same error during the first migration? If it had, i wouldn't have a gazillion columns to fix now :-/
NeoN0x commentedon Dec 16, 2020
I got the same issue.
I'm doing a migration that adds a new column, update the information, and set the column to not nullable.
But when splitting this, it seems to work.
lilhamad commentedon Jun 24, 2021
MY OWN ISSUE WAS :
I didn't change
allowNull: false,allowNull: true,on my model.
adi-manna commentedon Sep 24, 2021
sequelize's version: 6.6.5
I think the issue was fixed because it worked for me:
(both up and down functions)
The 'references' is not needed it's just creating more and more FKs as you can see here:
phu-viet-dang commentedon May 7, 2023
If you guys want to change column that also a foreignKey, do not include the references, only type and allowNull is enough