question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

schema autosync vs migrations

See original GitHub issue

Issue type:

[x ] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] websql

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Hi there,

I might be missing something, but it seems to me that the synchronize: true setting is not compatible with the concept of migrations in typeorm.

I took an existing project that uses typeorm and added a new entity. Being used to Rails’ ActiveRecord, I started adding the migration to create the relation, I then proceeded to write the entity (sometimes I do in the reverse order, but that doesn’t matter here).

I then ran the yarn typeorm migrations:run command and it correctly ran the migration and created the relation. When I started the node app though, I started seeing a lot of SQL logs and just moments afterwards errors of fields already existing. It took some time for me to figure out it was the autosync trying to sync the entities to the (already existing) relations.

I don’t understand the autosync very well yet. I don’t think it’d be useful for production use right? You can’t just automagically let a diff algorithm update your schema, it might result in loss of data. Migrations are needed to safely re-structure the schema and data. Why would someone use autosync? I’m looking for real-world cases.

In my case, I think I’ll just disable it and use migrations instead. I’m still looking to hear your opinions about best-practices here though.

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pleerockcommented, Feb 5, 2018

You can’t just automagically let a diff algorithm update your schema, it might result in loss of data.

Correct.

Migrations are needed to safely re-structure the schema and data.

Correct, if you manually write migrations. There is a migration auto-generation feature (now works only in mysql) which does the same as synchronize: true but it writes generation logic into migration file. Such approach also is not safe and you should be careful with it.

synchronize: true is a feature mostly for development purpose. Its easy to do development with this feature, you don’t need to care about your schema. It can also be used in production, but you need to be careful with it, use it when you are sure you did not make any changes in your entities which may lead to data loose.

In development I always use synchronize: true and in production I use hybrid approach - use schema sync when its safe to do, and use manual data correction before schema sync when I need to correct my data.

0reactions
bielscommented, Nov 19, 2020

Are generated migrations supported in postgres in typeorm v0.2.29 at the moment? Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrations Over Synchronize in TypeORM | by Uthpala Pitawela
Synchronize makes the entity sync with the database every time you run your application. Hence, whenever you add columns to an entity, create...
Read more >
Smooth Schema Syncs for Data Modeling and Migrations
Data changes all the time. Updating models, adding new fields due to new features or tooling, clarifying column names, cleaning unnecessary ...
Read more >
Sequelize.js: how to use migrations and sync - Stack Overflow
My thought process: at every stage, the initial database plus each migration in sequence should equal (plus or minus data) the database ......
Read more >
Your migrations are bad and you should feel bad
Schema changes, schema migrations, whatever you want to call them. ... Good programmers worry about data structures and their relationships.
Read more >
Schema migration requirements - HCL Product Documentation
In general, for DB2, MySQL, and SQL server databases, there are no additional settings or configuration required. However, there are some ground rules...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found