schema autosync vs migrations
See original GitHub issueIssue 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:
- Created 6 years ago
- Reactions:6
- Comments:6 (3 by maintainers)
Correct.
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.Are generated migrations supported in postgres in typeorm v0.2.29 at the moment? Thanks