Android GreenDAO generates optional ID property

this is my circuit generation code:

    Schema schema = new Schema(VERSION, "com.example.dao");

    Entity player = schema.addEntity("Player");
    Property playerIdProperty = player.addStringProperty("id").primaryKey().getProperty();
    player.addStringProperty("name").notNull();
    player.addStringProperty("created_at");
    player.addStringProperty("updated_at");

    Entity match = schema.addEntity("Match");
    match.addStringProperty("id").primaryKey();
    match.addIntProperty("score1");
    match.addIntProperty("score2");
    match.addStringProperty("created_at");
    match.addStringProperty("updated_at");

    match.addToOne(player, playerIdProperty, "dp1");
    match.addToOne(player, playerIdProperty, "dp2");
    match.addToOne(player, playerIdProperty, "op1");
    match.addToOne(player, playerIdProperty, "op2");


    new DaoGenerator().generateAll(schema, "app/src-gen");

And here is what it generates:

public class MatchDao extends AbstractDao<Match, String> {

public static final String TABLENAME = "MATCH";

/**
 * Properties of entity Match.<br/>
 * Can be used for QueryBuilder and for referencing column names.
*/
public static class Properties {
    public final static Property Id = new Property(0, String.class, "id", true, "ID");
    public final static Property Score1 = new Property(1, Integer.class, "score1", false, "SCORE1");
    public final static Property Score2 = new Property(2, Integer.class, "score2", false, "SCORE2");
    public final static Property Created_at = new Property(3, String.class, "created_at", false, "CREATED_AT");
    public final static Property Updated_at = new Property(4, String.class, "updated_at", false, "UPDATED_AT");
    public final static Property Id = new Property(5, String.class, "id", true, "ID");
};

As you can see, there are two properties in MatchDao called "Id". I need to create two tables with primary keys, which are rows (a row is a requirement of a remote db) and 4 foreign keys (there are 4 players in each match).

Question: why is the "Id" property duplicated and how to avoid it? thanks in advance

+4
source share
3 answers

I made the same mistake as you. You are currently doing the following:

Entity player = schema.addEntity("Player");
Property playerIdProperty = player.addStringProperty("id").primaryKey().getProperty();
...
match.addToOne(player, playerIdProperty, "dp1");

But you must add the playerId property to the target class:

Entity player = schema.addEntity("Player");
player.addStringProperty("id").primaryKey();
...
Entity match = schema.addEntity("Match");
Property player1IdProperty = match.addLongProperty("dp1").getProperty();
...
match.addToOne(player, player1IdProperty);

Hope this helps!

+4
source

, id . "id" 1:1 "Player". , addToOne(Entity target, Property fkProperty, java.lang.String name).

0

Maybe the addToOne function expects an integer property? I recommend that you follow these steps:

  • Change your primary key. This will be the foreign key:

    Property playerIdProperty = player.addIdProperty().autoincrement().primaryKey().getProperty();`
    
  • Create your unique string id:

    player.addStringProperty("identifier").notNull().unique();
    

However, you still have two attributes, but I think the model is better. I am for using a numeric identifier. Sorry for my English.

0
source

Source: https://habr.com/ru/post/1545085/


All Articles