Results 1 to 10 of 10

Thread: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

  1. #1
    Join Date
    Jul 2008
    Location
    Spain
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    Some fields in one PostgreSQL table are configured with "NOT NULL".
    Inside my Qt 4.3 application I try to detect this constraint, but I am trying with
    Qt Code:
    1. if(my_model.requiredStatus == QSqlField::Required)
    2. // Warn user that this field must be filled
    To copy to clipboard, switch view to plain text mode 
    But it allways returns -1 (QSqlField::Unknown).
    If this is not the method to follow, how can PostgreSQL tell to Qt when a field has some restriction (check, not null...)?
    Auryn
    Starting to learn the world of Qt

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    How do you create that my_model object?

  3. #3
    Join Date
    Jul 2008
    Location
    Spain
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    Here is a sample code showing how I am creating the model:
    Qt Code:
    1.  
    2. my_model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    3. my_model.setTable("my_postgresql_table");
    4.  
    5. my_model.setHeaderData(0, Qt::Horizontal, t("my_field_0"));
    6.  
    7. my_model.setHeaderData(1, Qt::Horizontal, t("my_field_1"));
    8. my_model.setRelation(1, QSqlRelation("foreign_table", "id", "caption");
    9.  
    10. my_model.select();
    11.  
    12. my_table.setModel(my_model);
    13. my_table.setItemDelegate(new QSqlRelationalDelegate(my_table));
    To copy to clipboard, switch view to plain text mode 

    The signal dataChanged is connected to one slot that calls:
    Qt Code:
    1. my_model->submitAll();
    To copy to clipboard, switch view to plain text mode 
    This works, but if one constraint (as NOT NULL) doesn't match, the line doesn't get inserted, and doesn't warn the user.

    Here is the code where I connect to my PostgreSQL database (before creating the model):
    Qt Code:
    1. my_conection = new QSqlDatabase(QSqlDatabase::addDatabase("QPSQL"));
    2. my_conection->setHostName("my_host");
    3. my_conection->setDatabaseName("my_postgresql_database");
    4. my_conection->setUserName("my_username");
    5. my_conection->setPassword("my_password");
    To copy to clipboard, switch view to plain text mode 

    Thank you
    Auryn
    Starting to learn the world of Qt

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    But requiredStatus() is a method of QSqlField class, not QSqlRelationalTableModel. You can get one through QSqlRecord class.

  5. #5
    Join Date
    Jul 2008
    Location
    Spain
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    Sorry, what I wanted to say at the beginning was this:

    Qt Code:
    1. if(my_model.record().field(0).requiredStatus() == QSqlField::Required)
    2. // Warn user that this field must be filled
    To copy to clipboard, switch view to plain text mode 

    It allways returns -1, so the field never is considered as required (unless it is required for PostgreSQL: ALTER TABLE my_table ALTER COLUMN first_field SET NOT NULL).

    In this way, I think my_model.record().field(0) is a QSqlField.
    Auryn
    Starting to learn the world of Qt

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    What does my_model.record().count() return?

  7. #7
    Join Date
    Jul 2008
    Location
    Spain
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    The sentence:
    Qt Code:
    1. my_model.record().count()
    To copy to clipboard, switch view to plain text mode 
    returns four (the correct number of columns in the table).
    Auryn
    Starting to learn the world of Qt

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    So it seems that everything is OK on your side and the problem is with the driver. Since you have table and field names (or at least you can get them from Qt), you can write your own method that will check that. You have to query the information_schema.columns.

    Something like this should do (if you don't use schemas):
    sql Code:
    1. SELECT ( is_nullable = 'NO' AND column_default IS NULL ) AS required
    2. FROM information_schema.COLUMNS
    3. WHERE table_name = :table_name AND column_name = :column_name;
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to jacek for this useful post:

    Auryn (2nd September 2008)

  10. #9
    Join Date
    Jul 2008
    Location
    Spain
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    Thank you, this method works to check "NOT NULL" constraint:

    Qt Code:
    1. bool My_class::Is_field_required(const QString &my_table, const QString &my_field) const
    2. {
    3. QSqlQuery my_query("SELECT(is_nullable='NO' AND column_default IS NULL) AS required FROM information_schema.COLUMNS WHERE table_name='"
    4. + my_table + "' AND column_name='" + my_field + "';");
    5. if (!my_query.exec())
    6. {
    7. return(false);
    8. }
    9. else
    10. {
    11. consulta.first();
    12. return(my_query.value(0).toBool());
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    But, what about other constraints (CHECK)?

    I supose that I must search inside information_schema too.

    SQL table example:
    Qt Code:
    1. CREATE TABLE my_products (
    2. my_product integer,
    3. my_product_name text,
    4. my_price numeric CHECK (my_price > 0)
    5. );
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 2nd September 2008 at 21:58. Reason: wrapped too long line
    Auryn
    Starting to learn the world of Qt

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: requiredStatus on Qt 4.3 and PostgreSQL 8.3.3

    Quote Originally Posted by Auryn View Post
    QSqlQuery my_query("SELECT(is_nullable='NO' AND column_default IS NULL) AS required FROM information_schema.COLUMNS WHERE table_name='"
    + my_table + "' AND column_name='" + my_field + "';");
    Better use QSqlQuery::prepare() and QSqlQuery::bindValue().


    Quote Originally Posted by Auryn View Post
    I supose that I must search inside information_schema too.
    Yes, see information_schema.check_constraints.

  12. The following user says thank you to jacek for this useful post:

    Auryn (3rd September 2008)

Similar Threads

  1. Qt and PostgreSQL : version not supported
    By xinevil in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2008, 13:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.