typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

What's the difference between query.changeColumn and query.renameColumn?

Open fwh1990 opened this issue 8 years ago • 12 comments

Issue type:

[ ] question

Database system/driver:

[ ] mysql

TypeORM version:

[ ] latest

Steps to reproduce or a small repository showing the problem:

Today, I want to change comment description of one column, so I use query.changeColumn(), it seems so right, but not as my expect. Framework drop my column and create a new for me. It's unnecessary exactly.

Then, I use query.renameColumn(), I got the right response. But, don't u think it's strange? It seems just will rename my column name.

fwh1990 avatar Apr 20 '18 08:04 fwh1990

I don't think drop and create is the best way to migrate. You will delete all data in this column, that's very very dangerous operation. May refactor this method to: dropAndCreateColumn()

fwh1990 avatar Apr 20 '18 08:04 fwh1990

I want to change comment description of one column, so I use query.changeColumn(), it

do you only change the name? Are you sure about it?

pleerock avatar Apr 23 '18 06:04 pleerock

I don't want to change my column's name, I want to change my column's comment schema. I think, or somebody also think, we should use queryRunner.changeCloumn().

But now, we can only use queryRunner.renameCloumn(), because queryRunner.changeCloumn() will drop my column at first, and then create it again.

fwh1990 avatar Apr 23 '18 06:04 fwh1990

The method queryRunner.renameCloumn() seems like a method only change column name. But it can do more things now. Maybe the method name renameCloumn is not proper here.

fwh1990 avatar Apr 23 '18 06:04 fwh1990

because queryRunner.changeCloumn() will drop my column at first, and then create it again.

everything is much more complicated as it may seem. There are reasons behind this behaviour. Column is dropped if you change type or length, because its required or operation may fail. If you only change name it will simply rename it.

pleerock avatar Apr 23 '18 07:04 pleerock

That may be dangerous. You can't drop column because the data in this column will gone, and the developers may don't realize that. So I suggest you refactor the method's name.

If I just change length from 20 to 30, or change type from int to float, that's ok. When migrate fail, the developers should migrate the data by themselves before they change the column schema.

Just don't drop column dangerously.

If somebody who really want to drop it's column, he can use queryRunner.dropColumn.

fwh1990 avatar Apr 23 '18 07:04 fwh1990

I agree, I had the same issue few days ago, luckily we found out this behaviour in the logs before applying migrations to production but the naming was making me think of an ALTER command.

sabau avatar Oct 16 '18 13:10 sabau

I'm closing this issue for housekeeping purposes, if something is not clear yet, just continue the conversation or post a message in slack

havenchyk avatar Dec 10 '18 16:12 havenchyk

The method queryRunner.renameCloumn() seems like a method only change column name. But it can do more things now. Maybe the method name renameCloumn is not proper here.

@fwh1990 You mentioned that renameColumn can do more things. What else can it do other than change column name? Any suggestion is appreciated.

jyang0110 avatar Mar 22 '19 02:03 jyang0110

Agree, this almost just caused a catastrophe in our application. The naming is counter-intuitive. It should be something like replaceColumn. The change column implies an update/change to a column in place. Further, the SQL CHANGE COLUMN command is an update that would not lose data. It stands that anyone familiar with SQL would expect changeColumn would be executing a CHANGE COLUMN and NOT A DROP!!

greenreign avatar May 14 '20 14:05 greenreign

This is a huge trap, and the author seems don't want to fix it. Am I right? I turn to use sequelize these years, and have no idea what happened to typeorm.

And I will not reuse typeorm before it fixes this issue. Sorry.

fwh1990 avatar May 14 '20 17:05 fwh1990

+1

chief-austinc avatar Aug 26 '22 14:08 chief-austinc