最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How `typeorm migration:generate` command know that I am renaming a column? - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 6

Just 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

发布评论

评论列表(0)

  1. 暂无评论