PDA

View Full Version : Postgresql QSqlRelationalTableModel empty table



RolandHughes
12th November 2008, 18:18
Hello,

psql 8.3.4
Ubuntu 8.10 AMD 64-bit
Qt 4 that shipped with it and native C/C++ compiler

I have a fairly simple database I'm implementing to track expenses.

START TRANSACTION;

DROP TABLE IF EXISTS expenses;
DROP TABLE IF EXISTS categories;
DROP TABLE IF EXISTS payees;


COMMIT;

START TRANSACTION;

CREATE TABLE categories (
category char(25) CONSTRAINT category_constraint NOT NULL PRIMARY KEY,
description char(50),
tax_ded boolean);

CREATE TABLE payees (
payee char(50) CONSTRAINT payee_constraint NOT NULL PRIMARY KEY,
contact char(50));

COMMIT;

START TRANSACTION;
--
-- No index or key on transaction table.
-- If this were a real-time system rather than a system used
-- at the end of the year in a mad rush we would use timestamp instead of date
-- data type and make this column the primary key.
--
CREATE TABLE expenses (
--tran_id serial,
tran_dt date,
category char(25) CONSTRAINT valid_cat REFERENCES categories (category) MATCH FULL ON DELETE RESTRICT,
tax_ded boolean,
payee char(50) CONSTRAINT valid_payee REFERENCES payees (payee) MATCH FULL ON DELETE RESTRICT,
amount numeric(10,2) CONSTRAINT amt_constraint NOT NULL);

COMMIT;

I have taken much of the code from the chap13 programs and written a simple entry screen to maintain the expense table.

If I start with a completely empty table, the row index is always -1 and the additions do not work.

void XpnsForm::addXpns()
{
int row = mapper->currentIndex();
qDebug() << "Current index " << row;
mapper->submit();
tableModel->insertRow(row);
mapper->setCurrentIndex(row);

amountLineEdit->clear();
tran_dtDateEdit->setDate(QDate::currentDate());
tran_dtDateEdit->setFocus();
}

roland@roland-desktop:~/xpnsqt$ ./xpnsqt
dbName: "tax_2138"
Current index -1
Current index -1

tax_2138=# select * from expenses;
tran_dt | category | tax_ded | payee | amount
---------+----------+---------+-------+--------
(0 rows)

If I add a bogus row to the table before running the program, all things work as planned.

tax_2138=# insert into expenses( tran_dt, category, tax_ded, payee, amount) values ('21380506', 'Gift', 'Y', 'IRS', 4444);
INSERT 0 1
tax_2138=# select * from expenses;
tran_dt | category | tax_ded | payee | amount
------------+---------------------------+---------+----------------------------------------------------+---------
2138-05-06 | Gift | t | IRS | 4444.00
2138-01-18 | Gift | | J C Whitney | 6666.00
2138-04-15 | Estimated Taxes - Federal | | IRS | 4455.00


Yes, I'm working with a future date table so if I forget to delete it I won't have a problem when I actually go to file taxes.

I'm still poking away at the problem, and may simply throw out using this tablemodel stuff to get a working application, but would like to know if others have encountered this. It is a most annoying problem.