PySide6.QtSql.QSqlRelationalTableModel¶
- class QSqlRelationalTableModel¶
- The - QSqlRelationalTableModelclass provides an editable data model for a single database table, with foreign key support.- Details- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - QSqlRelationalTableModelacts like- QSqlTableModel, but allows columns to be set as foreign keys into other database tables.  - The screenshot on the left shows a plain - QSqlTableModelin a QTableView. Foreign keys (- cityand- country) aren’t resolved to human-readable values. The screenshot on the right shows a- QSqlRelationalTableModel, with foreign keys resolved into human-readable text strings.- The following code snippet shows how the - QSqlRelationalTableModelwas set up:- model.setTable("employee") model.setRelation(2, QSqlRelation("city", "id", "name")) model.setRelation(3, QSqlRelation("country", "id", "name")) - The - setRelation()function calls establish a relationship between two tables. The first call specifies that column 2 in table- employeeis a foreign key that maps with field- idof table- city, and that the view should present the- city's- namefield to the user. The second call does something similar with column 3.- If you use a read-write - QSqlRelationalTableModel, you probably want to use- QSqlRelationalDelegateon the view. Unlike the default delegate,- QSqlRelationalDelegateprovides a combobox for fields that are foreign keys into other tables. To use the class, simply call QAbstractItemView::setItemDelegate() on the view with an instance of- QSqlRelationalDelegate:- std.unique_ptr<QTableView> view{QTableView()} view.setModel(model) view.setItemDelegate(QSqlRelationalDelegate(view.get())) - The relationaltablemodel example illustrates how to use - QSqlRelationalTableModelin conjunction with- QSqlRelationalDelegateto provide tables with foreign key support.  - Notes: - The table must have a primary key declared. 
- The table’s primary key may not contain a relation to another table. 
- If a relational table contains keys that refer to non-existent rows in the referenced table, the rows containing the invalid keys will not be exposed through the model. The user or the database is responsible for keeping referential integrity. 
- If a relation’s display column name is also used as a column name in the relational table, or if it is used as display column name in more than one relation it will be aliased. The alias is the relation’s table name, display column name and a unique id joined by an underscore (e.g. tablename_columnname_id). - fieldName()will return the aliased column name. All occurrences of the duplicate display column name are aliased when duplication is detected, but no aliasing is done to the column names in the main table. The aliasing doesn’t affect- QSqlRelation, so- displayColumn()will return the original display column name.
- The reference table name is aliased. The alias is the word “relTblAl” and the relationed column index joined by an underscore (e.g. relTblAl_2). The alias can be used to filter the table (For example, setFilter(“relTblAl_2=’Oslo’ OR relTblAl_3=’USA’”)). 
- When using - setData()the role should always be Qt::EditRole, and when using- data()the role should always be Qt::DisplayRole.
 - See also - QSqlRelation- QSqlRelationalDelegateRelational Table Model Example- Synopsis¶- Methods¶- def - __init__()
- def - relation()
- def - setJoinMode()
 - Virtual methods¶- def - relationModel()
- def - setRelation()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - class JoinMode¶
- Constant - Description - QSqlRelationalTableModel.JoinMode.InnerJoin - Inner join mode, return rows when there is at least one match in both tables. 
 - QSqlRelationalTableModel.JoinMode.LeftJoin - Left join mode, returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2). 
 - See also 
 - __init__([parent=None[, db=QSqlDatabase()]])¶
- Parameters:
- parent – - QObject
- db – - QSqlDatabase
 
 
 - Creates an empty - QSqlRelationalTableModeland sets the parent to- parentand the database connection to- db. If- dbis not valid, the default database connection will be used.- relation(column)¶
- Parameters:
- column – int 
- Return type:
 
 - Returns the relation for the column - column, or an invalid relation if no relation is set.- See also - relationModel(column)¶
- Parameters:
- column – int 
- Return type:
 
 - Returns a - QSqlTableModelobject for accessing the table for which- columnis a foreign key, or- Noneif there is no relation for the given- column.- The returned object is owned by the - QSqlRelationalTableModel.- See also - Sets the SQL - joinModeto show or hide rows with NULL foreign keys. In- InnerJoinmode (the default) these rows will not be shown: use the- LeftJoinmode if you want to show them.- See also - setRelation(column, relation)¶
- Parameters:
- column – int 
- relation – - QSqlRelation
 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Lets the specified - columnbe a foreign index specified by- relation.- Example: - model.setTable("employee") model.setRelation(2, QSqlRelation("city", "id", "name")) - The setRelation() call specifies that column 2 in table - employeeis a foreign key that maps with field- idof table- city, and that the view should present the- city's- namefield to the user.- Note: The table’s primary key may not contain a relation to another table. - See also