In typeorm, if I rename an entity's column from:
@Column()
address: string
to
@Column()
userAddress: string
Does it know that I am renaming a column when I run the mand
typeorm migration:generate
?
Will I lose any data when I run the migration?
In typeorm, if I rename an entity's column from:
@Column()
address: string
to
@Column()
userAddress: string
Does it know that I am renaming a column when I run the mand
typeorm migration:generate
?
Will I lose any data when I run the migration?
Share Improve this question asked Oct 21, 2020 at 14:22 Nhan NguyenNhan Nguyen 3173 silver badges9 bronze badges 1- Share the generated migration file. It will tell you exactly what is going to happen – Jesse Carter Commented Oct 21, 2020 at 15:01
3 Answers
Reset to default 6Just for the sake if other people should visit the question at some point. (I realize it's a old question)
TypeORM does NOT recognize it, and will try to drop the columns with data and create a new one. Often this poses conflicts if it the column is not nullable or worse drop tons of data silently.
Make sure to either configure the migration generated or write your own in this case.
This answer will help anyone, who wants to do the same. In the name
you need to provide a real column name in the DB. Then below just provide an alias. It will look like this:
@Column({ name: 'address' })
userAddress: string
The mand migrations:generate will go over the entities files provided in the configurations file, and will make a new migrations file with the changes made for you. So, if you run this mand after every change made, you wont lose data. Ofcourse that if you drop columns/tables you will lose its data because you delete it.
Make sure to have all the paths in the 'entities' paths array of the typeorm.config file