Room database in conflict is not working, data always has been inserted












-1















I am trying to fetch some data from API and save it to room database, but the idea I have a column and I am changing its value "isFavourite".



I saw in some answers about this to use upsert function, and I tried to use it but still overriding data everytime.



@Dao
public abstract class ChannelDao {

@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract long addChannelData(Channel channel);

@Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

@Transaction
void insOrUpdate(Channel channel) {
long id = addChannelData(channel);
if (id == -1) {
updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
}
}


@Entity(tableName = "channel_table", foreignKeys = @ForeignKey(entity = ChannelsPackage.class, parentColumns = "id", childColumns = "package_id", onDelete = CASCADE))

public class Channel {

@NonNull
@PrimaryKey
@SerializedName("number")
@Expose
private String number;
@SerializedName("name")
@Expose
private String name;
@SerializedName("url")
@Expose
private String url;
@SerializedName("icon")
@Expose
private String icon;
@SerializedName("streamer")
@Expose
private String streamer;

private boolean favorite;

private boolean locked;

private String package_id;

public Channel(@NonNull String number, String name, String url, String icon, String streamer, boolean favorite, boolean locked, String package_id) {
this.number = number;
this.name = name;
this.url = url;
this.icon = icon;
this.streamer = streamer;
this.favorite = favorite;
this.locked = locked;
this.package_id = package_id;
}
@NonNull
public String getNumber() {
return number;
}

public void setNumber(@NonNull String number) {
this.number = number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

public String getStreamer() {
return streamer;
}

public void setStreamer(String streamer) {
this.streamer = streamer;
}

public boolean isFavorite() {
return favorite;
}

public void setFavorite(boolean favorite) {
this.favorite = favorite;
}

public boolean isLocked() {
return locked;
}

public void setLocked(boolean locked) {
this.locked = locked;
}

public String getPackage_id() {
return package_id;
}

public void setPackage_id(String package_id) {
this.package_id = package_id;
}
}









share|improve this question




















  • 1





    I noticed number is your primary key, which means if the data has a different number even if all the other attributes are the same, it will be inserted. Or, the conflict strategy take into account only the primary.

    – Dr4ke the b4dass
    Nov 21 '18 at 1:14











  • the number is String and it's unique from server side as well

    – user3614504
    Nov 21 '18 at 9:52


















-1















I am trying to fetch some data from API and save it to room database, but the idea I have a column and I am changing its value "isFavourite".



I saw in some answers about this to use upsert function, and I tried to use it but still overriding data everytime.



@Dao
public abstract class ChannelDao {

@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract long addChannelData(Channel channel);

@Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

@Transaction
void insOrUpdate(Channel channel) {
long id = addChannelData(channel);
if (id == -1) {
updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
}
}


@Entity(tableName = "channel_table", foreignKeys = @ForeignKey(entity = ChannelsPackage.class, parentColumns = "id", childColumns = "package_id", onDelete = CASCADE))

public class Channel {

@NonNull
@PrimaryKey
@SerializedName("number")
@Expose
private String number;
@SerializedName("name")
@Expose
private String name;
@SerializedName("url")
@Expose
private String url;
@SerializedName("icon")
@Expose
private String icon;
@SerializedName("streamer")
@Expose
private String streamer;

private boolean favorite;

private boolean locked;

private String package_id;

public Channel(@NonNull String number, String name, String url, String icon, String streamer, boolean favorite, boolean locked, String package_id) {
this.number = number;
this.name = name;
this.url = url;
this.icon = icon;
this.streamer = streamer;
this.favorite = favorite;
this.locked = locked;
this.package_id = package_id;
}
@NonNull
public String getNumber() {
return number;
}

public void setNumber(@NonNull String number) {
this.number = number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

public String getStreamer() {
return streamer;
}

public void setStreamer(String streamer) {
this.streamer = streamer;
}

public boolean isFavorite() {
return favorite;
}

public void setFavorite(boolean favorite) {
this.favorite = favorite;
}

public boolean isLocked() {
return locked;
}

public void setLocked(boolean locked) {
this.locked = locked;
}

public String getPackage_id() {
return package_id;
}

public void setPackage_id(String package_id) {
this.package_id = package_id;
}
}









share|improve this question




















  • 1





    I noticed number is your primary key, which means if the data has a different number even if all the other attributes are the same, it will be inserted. Or, the conflict strategy take into account only the primary.

    – Dr4ke the b4dass
    Nov 21 '18 at 1:14











  • the number is String and it's unique from server side as well

    – user3614504
    Nov 21 '18 at 9:52
















-1












-1








-1








I am trying to fetch some data from API and save it to room database, but the idea I have a column and I am changing its value "isFavourite".



I saw in some answers about this to use upsert function, and I tried to use it but still overriding data everytime.



@Dao
public abstract class ChannelDao {

@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract long addChannelData(Channel channel);

@Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

@Transaction
void insOrUpdate(Channel channel) {
long id = addChannelData(channel);
if (id == -1) {
updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
}
}


@Entity(tableName = "channel_table", foreignKeys = @ForeignKey(entity = ChannelsPackage.class, parentColumns = "id", childColumns = "package_id", onDelete = CASCADE))

public class Channel {

@NonNull
@PrimaryKey
@SerializedName("number")
@Expose
private String number;
@SerializedName("name")
@Expose
private String name;
@SerializedName("url")
@Expose
private String url;
@SerializedName("icon")
@Expose
private String icon;
@SerializedName("streamer")
@Expose
private String streamer;

private boolean favorite;

private boolean locked;

private String package_id;

public Channel(@NonNull String number, String name, String url, String icon, String streamer, boolean favorite, boolean locked, String package_id) {
this.number = number;
this.name = name;
this.url = url;
this.icon = icon;
this.streamer = streamer;
this.favorite = favorite;
this.locked = locked;
this.package_id = package_id;
}
@NonNull
public String getNumber() {
return number;
}

public void setNumber(@NonNull String number) {
this.number = number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

public String getStreamer() {
return streamer;
}

public void setStreamer(String streamer) {
this.streamer = streamer;
}

public boolean isFavorite() {
return favorite;
}

public void setFavorite(boolean favorite) {
this.favorite = favorite;
}

public boolean isLocked() {
return locked;
}

public void setLocked(boolean locked) {
this.locked = locked;
}

public String getPackage_id() {
return package_id;
}

public void setPackage_id(String package_id) {
this.package_id = package_id;
}
}









share|improve this question
















I am trying to fetch some data from API and save it to room database, but the idea I have a column and I am changing its value "isFavourite".



I saw in some answers about this to use upsert function, and I tried to use it but still overriding data everytime.



@Dao
public abstract class ChannelDao {

@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract long addChannelData(Channel channel);

@Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

@Transaction
void insOrUpdate(Channel channel) {
long id = addChannelData(channel);
if (id == -1) {
updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
}
}


@Entity(tableName = "channel_table", foreignKeys = @ForeignKey(entity = ChannelsPackage.class, parentColumns = "id", childColumns = "package_id", onDelete = CASCADE))

public class Channel {

@NonNull
@PrimaryKey
@SerializedName("number")
@Expose
private String number;
@SerializedName("name")
@Expose
private String name;
@SerializedName("url")
@Expose
private String url;
@SerializedName("icon")
@Expose
private String icon;
@SerializedName("streamer")
@Expose
private String streamer;

private boolean favorite;

private boolean locked;

private String package_id;

public Channel(@NonNull String number, String name, String url, String icon, String streamer, boolean favorite, boolean locked, String package_id) {
this.number = number;
this.name = name;
this.url = url;
this.icon = icon;
this.streamer = streamer;
this.favorite = favorite;
this.locked = locked;
this.package_id = package_id;
}
@NonNull
public String getNumber() {
return number;
}

public void setNumber(@NonNull String number) {
this.number = number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

public String getStreamer() {
return streamer;
}

public void setStreamer(String streamer) {
this.streamer = streamer;
}

public boolean isFavorite() {
return favorite;
}

public void setFavorite(boolean favorite) {
this.favorite = favorite;
}

public boolean isLocked() {
return locked;
}

public void setLocked(boolean locked) {
this.locked = locked;
}

public String getPackage_id() {
return package_id;
}

public void setPackage_id(String package_id) {
this.package_id = package_id;
}
}






android android-room android-architecture-components android-livedata






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 1:07









shizhen

2,7903830




2,7903830










asked Nov 21 '18 at 1:06









user3614504user3614504

156




156








  • 1





    I noticed number is your primary key, which means if the data has a different number even if all the other attributes are the same, it will be inserted. Or, the conflict strategy take into account only the primary.

    – Dr4ke the b4dass
    Nov 21 '18 at 1:14











  • the number is String and it's unique from server side as well

    – user3614504
    Nov 21 '18 at 9:52
















  • 1





    I noticed number is your primary key, which means if the data has a different number even if all the other attributes are the same, it will be inserted. Or, the conflict strategy take into account only the primary.

    – Dr4ke the b4dass
    Nov 21 '18 at 1:14











  • the number is String and it's unique from server side as well

    – user3614504
    Nov 21 '18 at 9:52










1




1





I noticed number is your primary key, which means if the data has a different number even if all the other attributes are the same, it will be inserted. Or, the conflict strategy take into account only the primary.

– Dr4ke the b4dass
Nov 21 '18 at 1:14





I noticed number is your primary key, which means if the data has a different number even if all the other attributes are the same, it will be inserted. Or, the conflict strategy take into account only the primary.

– Dr4ke the b4dass
Nov 21 '18 at 1:14













the number is String and it's unique from server side as well

– user3614504
Nov 21 '18 at 9:52







the number is String and it's unique from server side as well

– user3614504
Nov 21 '18 at 9:52














2 Answers
2






active

oldest

votes


















0














Change to onConflictStrategy.ABORT. This way it will abort the transaction.






share|improve this answer































    0














    After a search and search for 4 days, found the issue,
    I have 2 tables channel_table and package_table



    and the relation between them was one too many, and I was adding the packages every time before the channels and the was "onDelete = CASCADE"
    so that's why all channels were deleted every time I am trying to update.



    and for info to make upsert method it will be like this:



    @Insert(onConflict = OnConflictStrategy.REPLACE)
    abstract void addChannelData(Channel channel);

    @Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
    abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

    @Transaction
    @Query("SELECT COUNT(*) from channel_table WHERE number = :number")
    abstract int isChannelExist(String number);

    @Transaction
    void insOrUpdate(List<Channel> channels) {
    for (Channel channel : channels) {
    if (isChannelExist(channel.getNumber()) > 0) {
    updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
    } else {
    addChannelData(channel);
    }
    }

    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403902%2froom-database-in-conflict-is-not-working-data-always-has-been-inserted%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Change to onConflictStrategy.ABORT. This way it will abort the transaction.






      share|improve this answer




























        0














        Change to onConflictStrategy.ABORT. This way it will abort the transaction.






        share|improve this answer


























          0












          0








          0







          Change to onConflictStrategy.ABORT. This way it will abort the transaction.






          share|improve this answer













          Change to onConflictStrategy.ABORT. This way it will abort the transaction.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:20









          Dr4ke the b4dassDr4ke the b4dass

          154113




          154113

























              0














              After a search and search for 4 days, found the issue,
              I have 2 tables channel_table and package_table



              and the relation between them was one too many, and I was adding the packages every time before the channels and the was "onDelete = CASCADE"
              so that's why all channels were deleted every time I am trying to update.



              and for info to make upsert method it will be like this:



              @Insert(onConflict = OnConflictStrategy.REPLACE)
              abstract void addChannelData(Channel channel);

              @Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
              abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

              @Transaction
              @Query("SELECT COUNT(*) from channel_table WHERE number = :number")
              abstract int isChannelExist(String number);

              @Transaction
              void insOrUpdate(List<Channel> channels) {
              for (Channel channel : channels) {
              if (isChannelExist(channel.getNumber()) > 0) {
              updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
              } else {
              addChannelData(channel);
              }
              }

              }





              share|improve this answer




























                0














                After a search and search for 4 days, found the issue,
                I have 2 tables channel_table and package_table



                and the relation between them was one too many, and I was adding the packages every time before the channels and the was "onDelete = CASCADE"
                so that's why all channels were deleted every time I am trying to update.



                and for info to make upsert method it will be like this:



                @Insert(onConflict = OnConflictStrategy.REPLACE)
                abstract void addChannelData(Channel channel);

                @Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
                abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

                @Transaction
                @Query("SELECT COUNT(*) from channel_table WHERE number = :number")
                abstract int isChannelExist(String number);

                @Transaction
                void insOrUpdate(List<Channel> channels) {
                for (Channel channel : channels) {
                if (isChannelExist(channel.getNumber()) > 0) {
                updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
                } else {
                addChannelData(channel);
                }
                }

                }





                share|improve this answer


























                  0












                  0








                  0







                  After a search and search for 4 days, found the issue,
                  I have 2 tables channel_table and package_table



                  and the relation between them was one too many, and I was adding the packages every time before the channels and the was "onDelete = CASCADE"
                  so that's why all channels were deleted every time I am trying to update.



                  and for info to make upsert method it will be like this:



                  @Insert(onConflict = OnConflictStrategy.REPLACE)
                  abstract void addChannelData(Channel channel);

                  @Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
                  abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

                  @Transaction
                  @Query("SELECT COUNT(*) from channel_table WHERE number = :number")
                  abstract int isChannelExist(String number);

                  @Transaction
                  void insOrUpdate(List<Channel> channels) {
                  for (Channel channel : channels) {
                  if (isChannelExist(channel.getNumber()) > 0) {
                  updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
                  } else {
                  addChannelData(channel);
                  }
                  }

                  }





                  share|improve this answer













                  After a search and search for 4 days, found the issue,
                  I have 2 tables channel_table and package_table



                  and the relation between them was one too many, and I was adding the packages every time before the channels and the was "onDelete = CASCADE"
                  so that's why all channels were deleted every time I am trying to update.



                  and for info to make upsert method it will be like this:



                  @Insert(onConflict = OnConflictStrategy.REPLACE)
                  abstract void addChannelData(Channel channel);

                  @Query("UPDATE channel_table SET name= :name, url= :url , icon= :icon , streamer= :streamer , package_id= :package_id WHERE number= :number")
                  abstract void updateChannelData(String number, String name, String url, String icon, String streamer, String package_id);

                  @Transaction
                  @Query("SELECT COUNT(*) from channel_table WHERE number = :number")
                  abstract int isChannelExist(String number);

                  @Transaction
                  void insOrUpdate(List<Channel> channels) {
                  for (Channel channel : channels) {
                  if (isChannelExist(channel.getNumber()) > 0) {
                  updateChannelData(channel.getNumber(), channel.getName(), channel.getUrl(), channel.getIcon(), channel.getStreamer(), channel.getPackage_id());
                  } else {
                  addChannelData(channel);
                  }
                  }

                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 19:26









                  user3614504user3614504

                  156




                  156






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403902%2froom-database-in-conflict-is-not-working-data-always-has-been-inserted%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]