pawelo
pawelo

Reputation: 1415

Android GreenDAO generates additional ID property

this is my schema 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 this 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, in MatchDao there is two properties called "Id". What I need to do is to generate two tables with primary keys being strings (string is remote db's requirement) and 4 foreign keys (each Match has 4 players).

The question is: why "Id" property is duplicated and how to avoid it? Thanks in advance

Upvotes: 4

Views: 2014

Answers (2)

gfhuertac
gfhuertac

Reputation: 366

I made the same mistake as you before. Currently you are doing the following:

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

But you have to 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!

Upvotes: 4

jcasero
jcasero

Reputation: 46

I am not sure but this id property is not duplicate. This "id" property is result from the 1:1 relation with the entity "Player". Perhaps there is a bug in addToOne(Entity target, Property fkProperty, java.lang.String name) method.

Upvotes: 0

Related Questions