Skip to content

Commit

Permalink
Avoid "FOREIGN KEY constraint failed" for account.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Jan 6, 2025
1 parent 02a4f56 commit 1425ef6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class DatabaseHelper extends SQLiteOpenHelper {
+ AccountEntry.COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID + " varchar(255), "
+ AccountEntry.COLUMN_CREATED_AT + " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "
+ AccountEntry.COLUMN_MODIFIED_AT + " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "
// + "FOREIGN KEY (" + AccountEntry.COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID + ") REFERENCES " + AccountEntry.TABLE_NAME + " (" + AccountEntry.COLUMN_UID + ") ON DELETE SET NULL, "
+ "FOREIGN KEY (" + AccountEntry.COLUMN_COMMODITY_UID + ") REFERENCES " + CommodityEntry.TABLE_NAME + " (" + CommodityEntry.COLUMN_UID + ") "
+ ");" + createUpdatedAtTrigger(AccountEntry.TABLE_NAME);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public class AccountFormFragment extends MenuFragment implements FragmentResultL
/**
* Accounts database adapter
*/
private AccountsDbAdapter mAccountsDbAdapter;
private final AccountsDbAdapter mAccountsDbAdapter = AccountsDbAdapter.getInstance();
private final CommoditiesDbAdapter commodityDbAdapter = CommoditiesDbAdapter.getInstance();

/**
* GUID of the parent account
Expand Down Expand Up @@ -163,15 +164,12 @@ public AccountFormFragment() {
* @return New instance of the dialog fragment
*/
static public AccountFormFragment newInstance() {
AccountFormFragment f = new AccountFormFragment();
f.mAccountsDbAdapter = AccountsDbAdapter.getInstance();
return f;
return new AccountFormFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAccountsDbAdapter = AccountsDbAdapter.getInstance();
mUseDoubleEntry = GnuCashApplication.isDoubleEntryEnabled();
mAccountUID = getArguments().getString(UxArgument.SELECTED_ACCOUNT_UID);
}
Expand Down Expand Up @@ -305,7 +303,7 @@ private void initializeViewsWithAccount(Account account) {
String currencyCode = account.getCommodity().getCurrencyCode();
setSelectedCurrency(currencyCode);

if (mAccountsDbAdapter.getTransactionMaxSplitNum(mAccount.getUID()) > 1) {
if (mAccountsDbAdapter.getTransactionMaxSplitNum(account.getUID()) > 1) {
//TODO: Allow changing the currency and effecting the change for all transactions without any currency exchange (purely cosmetic change)
mBinding.inputCurrencySpinner.setEnabled(false);
}
Expand Down Expand Up @@ -384,8 +382,6 @@ private void setDefaultTransferAccountInputsVisible(boolean visible) {
* @param currencyCode ISO 4217 currency code to be selected
*/
private void setSelectedCurrency(String currencyCode) {
CommoditiesDbAdapter commodityDbAdapter = CommoditiesDbAdapter.getInstance();
if (commodityDbAdapter == null) return;
long commodityId = commodityDbAdapter.getID(commodityDbAdapter.getCommodityUID(currencyCode));
int position = 0;
for (int i = 0; i < mBinding.inputCurrencySpinner.getCount(); i++) {
Expand Down Expand Up @@ -675,29 +671,28 @@ public void onDestroy() {
*/
private void saveAccount() {
Timber.i("Saving account");
if (mAccountsDbAdapter == null)
mAccountsDbAdapter = AccountsDbAdapter.getInstance();

// accounts to update, in case we're updating full names of a sub account tree
ArrayList<Account> accountsToUpdate = new ArrayList<>();
boolean nameChanged = false;
String newName = getEnteredName();
if (TextUtils.isEmpty(newName)) {
mBinding.nameTextInputLayout.setErrorEnabled(true);
mBinding.nameTextInputLayout.setError(getString(R.string.toast_no_account_name_entered));
return;
} else {
mBinding.nameTextInputLayout.setError(null);
}
long commodityId = mBinding.inputCurrencySpinner.getSelectedItemId();
Commodity commodity = commodityDbAdapter.getRecord(commodityId);
if (mAccount == null) {
if (TextUtils.isEmpty(newName)) {
mBinding.nameTextInputLayout.setErrorEnabled(true);
mBinding.nameTextInputLayout.setError(getString(R.string.toast_no_account_name_entered));
return;
}
mAccount = new Account(newName);
mAccount = new Account(newName, commodity);
mAccountsDbAdapter.addRecord(mAccount, DatabaseAdapter.UpdateMethod.insert); //new account, insert it
} else {
nameChanged = !mAccount.getName().equals(newName);
mAccount.setName(newName);
mAccount.setCommodity(commodity);
}

long commodityId = mBinding.inputCurrencySpinner.getSelectedItemId();
Commodity commodity = CommoditiesDbAdapter.getInstance().getRecord(commodityId);
mAccount.setCommodity(commodity);

AccountType selectedAccountType = getSelectedAccountType();
mAccount.setAccountType(selectedAccountType);

Expand All @@ -710,7 +705,6 @@ private void saveAccount() {
if (mBinding.checkboxParentAccount.isChecked()) {
newParentAccountId = mBinding.inputParentAccount.getSelectedItemId();
newParentAccountUID = mAccountsDbAdapter.getUID(newParentAccountId);
mAccount.setParentUID(newParentAccountUID);
} else {
//need to do this explicitly in case user removes parent account
newParentAccountUID = mRootAccountUID;
Expand All @@ -729,6 +723,7 @@ private void saveAccount() {

long parentAccountId = mParentAccountUID == null ? -1 : mAccountsDbAdapter.getID(mParentAccountUID);
// update full names
List<Account> accountsToUpdate = new ArrayList<>();
if (nameChanged || mDescendantAccountUIDs == null || newParentAccountId != parentAccountId) {
// current account name changed or new Account or parent account changed
String newAccountFullName;
Expand Down

0 comments on commit 1425ef6

Please sign in to comment.