Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Ormlite upgrade issue with ForeignCollectionField
    primarykey
    data
    text
    <p>While upgrading my database schema I have run into a problem with a ForeignCollectionField (ormlite 4.7) not returning rows. With a clean install of the app, rows can be added and displayed as expected. </p> <p>When the app is updated to a new version, the schema is updated (see below), but when rows are added to the database the collection is not returning the added rows. (I can see the rows exist in the database)</p> <p>The parent row existed before the update. What do I need to do to fix it?</p> <p><strong>Parent class with Foreign Collection defined</strong></p> <pre><code>@DatabaseTable(tableName = "setting") public class SettingEntity { @DatabaseField(generatedId = true) private long id; … //New field added @ForeignCollectionField private ForeignCollection&lt;DistributionEntity&gt; distribution; public SettingEntity() { // Required for ORMLite } public ForeignCollection&lt;DistributionEntity&gt; getDistribution() { return distribution; } public void setDistribution(ForeignCollection&lt;DistributionEntity&gt; distribution) { this.distribution = distribution; } } </code></pre> <p><strong>Child class</strong></p> <pre><code>@DatabaseTable(tableName = "distribution") public class DistributionEntity { @DatabaseField(generatedId = true) private long id; … //New field added @DatabaseField(canBeNull = true, foreign = true, index = true, foreignAutoRefresh = true, columnDefinition = "integer references setting(id) on delete cascade") private SettingEntity setting; public void setSetting(SettingEntity setting) { this.setting = setting; } } </code></pre> <p><strong>onUpgrade code</strong></p> <pre><code>RuntimeExceptionDao&lt;DistributionEntity, Integer&gt; distributionDao = helper.getDistributionDao(); distributionDao.executeRaw("ALTER TABLE distribution ADD setting_id"); distributionDao.executeRaw("CREATE INDEX distribution_setting_idx ON distribution (setting_id)"); </code></pre> <p><strong>Debug info of the ForeignCollectionField call distribution</strong></p> <p><img src="https://i.stack.imgur.com/OHyTP.png" alt="enter image description here"></p> <p><strong>The code that iterates over the collection</strong></p> <pre><code>public ArrayList&lt;Distribution&gt; getDistribution() { getEntity().getDistribution().size(); final ArrayList&lt;Distribution&gt; items = new ArrayList&lt;Distribution&gt;(); final ForeignCollection&lt;DistributionEntity&gt; collection = getEntity().getDistribution(); for (final DistributionEntity item : collection) { final Distribution dist = new Distribution(item, mContext); items.add(dist); } return items; } </code></pre> <p>NB getEntity() returns an instance of SettingEntity</p> <p>Thanks for spending the time</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload