简体   繁体   English

ORMlite更改主键

[英]ORMlite alter primary key

is there a way, to change the primary key of ORMlite defined databases ? 有没有办法更改ORMlite定义的数据库的主键?

i'm having a class User with 我有一个班级用户

@DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false, id = true)
private String name = "";

and i query it with DAO.querybyid(String name); 我用DAO.querybyid(String name);

But when i try to alter the name in the database, it doesn't give me any warnings, will ORMLite alter the table column and remove the index, and then update the column and place the index again ? 但是,当我尝试更改数据库中的名称时,它没有发出任何警告,ORMLite会更改表列并删除索引,然后更新该列并再次放置索引吗?

I don't think so heh ? 我不这么认为吗? so need a workaround :( but also want the DAO.querybyid() advantage to get the user by String. 因此,需要一种解决方法:(,但也希望DAO.querybyid()能够通过String获得用户。

Can someone help me ? 有人能帮我吗 ?

will ORMLite alter the table column and remove the index, and then update the column and place the index again ? ORMLite是否会更改表列并删除索引,然后更新该列并再次放置索引?

I assume you are altering your schema using the dao.rawExecute(...) method ? 我假设您正在使用dao.rawExecute(...)方法更改架构? ORMLite doesn't do anything magical with this statement -- it just sends it to the underlying database. ORMLite对此语句并没有做任何神奇的事情,它只是将其发送到基础数据库。 So if you are asking for the field name but it's been renamed to be id then the DAO will not work since it doesn't correspond to the database. 因此,如果您要输入字段name但已将其重命名为id则DAO将不起作用,因为它与数据库不对应。 It will do a "SELECT name, ... FROM ..." and get a SQL error. 它将执行"SELECT name, ... FROM ..."并得到一个SQL错误。

What you can do is have two different versions of the class. 可以做的是拥有两个不同版本的类。 The first one has it with the "old" field-name and the new one has it with the "new" name. 第一个具有“旧”字段名称,而新的具有“新”名称。 Then if you detect that you need to update the schema, you can issue the raw ALTER SQL statements, and then start using the DAO corresponding to the new version of the class. 然后,如果检测到需要更新架构,则可以发出原始的ALTER SQL语句,然后开始使用与该类的新版本相对应的DAO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM