Does removing a property from a domain class cause an automatic update to the schema, whereby the corresponding column is dropped?

风流意气都作罢 提交于 2019-12-04 03:46:18

问题


I'm sort of new at Grails. I've worked with it a bit, but not that much. I'm pretty familiar with Java though. My question is regarding schema updates. I understand that Grails creates Hibernate mappings by looking at the domain classes, and so if I add a new property, Grails will automatically add a column for that property in the database. Does the reverse also hold true? If I remove a property, is that column removed? I'm not seeing that behavior and so I'm wondering if it is a configuration issue.

If I wanted to go into more robust database-management, I'm guessing I will have to use the database-management plugin or something like Liquibase. However, the project I'm working on is pretty simple and for the moment, we haven't decided if we are going in that direction yet.


回答1:


It depends on your dbCreate setting in DataSource.groovy. If it's create or create-drop then everything gets rebuilt when you restart. If it's update then new tables and columns get added. If it's some other setting then no changes are made.

update doesn't do what most people expect though. It's pessimistic and won't make changes that could result in data loss or corruption. So it won't change the size of a column even if it's wider (e.g. VARCHAR(50) -> VARCHAR(200)). It won't add indexes. It will add a new column that's specified as not-null, but it adds it as nullable since otherwise the previously inserted rows won't be valid. But it won't drop a column or table. So you can easily get into a scenario where you rename a column and end up with two - the old and the new.

Liquibase is a great library and the http://grails.org/plugin/database-migration is popular, so it's easy to get support for both. Once you get past the point in development when your schema stabilizes somewhat you should look into using the plugin.



来源:https://stackoverflow.com/questions/11445004/does-removing-a-property-from-a-domain-class-cause-an-automatic-update-to-the-sc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!