QSqlTableModel setPrimaryKey for a mysql view - qt

I have a QSqlTableModel that works to view/update a table in mysql. If I make a view of the table then it will display the data but edits fail. I can update the table with a mysql update so the view is updateable.
I think what is happening is that since the view does not have a primary key the primary key for the QSqlTableModel is not set and it won't update.
I can't find any examples how to set the primary key. The setPrimaryKey() of QSqlTableModel is protected. I am sure I am missing something fundamental but searching returns a lot of results for view in the context of MVC.
Can anyone point me to an example of how to call setPrimaryKey or an example of using a QSqlTableModel to update a database view in mysql?

I am thinking now that this is not going to work like I want it to anyway. I could probably make it work for a view with a single table but it would likely be too much work to make it behave properly if I have a view with multiple tables. It would be better to take a different approach.

Related

QSqlTableModel on multiple tables

I am using Qt's Model View Programming on a database, where an object is represented using multiple tables. Assume the following object and coresponding database tables which perfectly fit my design:
TagObject
- id
- name
- usable
- information
tag_table
- id
- name
- usable
tag_info_table
- id_ref
- info
As you may see, the information property is separated into another table to prevent existence of NULL because this property is optional.
In the database I have a view which aggregates the values into one 'table' which can be queried using QSqlTableModel. Note that INSERTing data is not possible this way. As far as I could understand, the database design is not supported by Qt's classes, neither QSqlTableModel nor QSqlRelationalTableModel do support this. (Additionally QSqlQueryModel does not support inserts at all so this is out of question.)
Am I missing something? Is there any way to do this using Qt's SQL classes? Or is the only way to achive this subclassing QSqlQueryModel as pointed out here?
The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.
Edit: As for subclassing I found this reference as a nice entry point.
Ideally, the view should have appropriate triggers that will modify the underlying tables. Make the view writable and your problems go away: you can use a QSqlTableModel directly on that view, then.
Alternatively, you can have a QSqlTableModel for each table, and then write a custom proxy model that supports inserts and translates between the source models and forms the writable view. It'll be more work than writing the SQL triggers.

Does Axapta 4.0 have a functionality to map data between different tables

I have two tables Cashdisc and Convertterm, Does axapta have a functionality where Convertterm.code can be related to CashDisc.Code so that whenever converterm.code changes it can be updated in CashDisc.Code.
Yes. I think you're referring to a table relation.
If you want a table to update another table, you can do that through code, but I think you're talking about how the table naturally functions with table relations.
See here https://msdn.microsoft.com/en-us/library/bb190076(v=ax.10).aspx
You need to write custom code on Convertterm update method after super to update the Cashdisc table.
If you create table relation in child table and if you set validate property to yes, then you can restrict child data creation without a parent record.

filling a grid with from two different record (table) sources

I was wondering if there is any way to populate a table using two different records. My records have the same primary keys, but when I am adding the specific fields to my grid I have this error:
More than one data (key) in one scroll.
I tried to make a Control view field in my primary record, and then refer the new record fields as relative fields but in this case the data is not populating in my grid. Any help or hint will be much appreciated.
I've only done this where the 2nd record is a Derived/Work record.
Can you create a view that combines both records, and put the view in the grid? FYI, peoplesoft let's you update the data in a view, which is not typical in an oracle db system.
Ok guys finally after all discussion, and much try rounds I figured out how to do this.
You need to add a draw a grid beside your old grid and populate it with a new record, which has dynamic view (dynamic sql). The only important case there, is you need to make sure the order of your fields in your records, are in the same order of your fields in the SELECT statement of your sql. Otherwise you will see
an SQL error
.
Try to make a field as display control field which is acting as primary key for its native table and as Foreign key for some other record.. A field needs to be made related if is being fetched from some other record... that is from the record which is not under current consideration. I think this way one can fetch data into grid or scroll respectively from multiple records.

Infragistics - Automatically create data schema but binding manually

I'm new to infragistics. I have a database and a datalayer with entity framework and linq. Now I want to get the data to an Infragisticscontrol, let's use a UltraCombo. I have not problem to bind the data to the control. My problem is that all properties from the linqquery is displayed.
As far as I can see there are two options
1) Create a data schema manually. In this way there is much effort put in creating the schema and when I refactor something then the schemas of all controls must be updated.
2) Creating a data schema automatically.
I played a bit with the secand case but can'T find an option how to create the data schema automatically but binding the data manually. I had one case where I bound it to a model of the entity framework and deleted the datasource later from the project. In this case it worked but I don't think that this is the right way to use it.
Can somebody tell me how to do this or what's the best practice?
I found a solution in the Infragistics forum below. Here's the link: http://blogs.infragistics.com/forums/p/24379/89509.aspx

ASP.Net Entity Framework Model

Is it possible to add properties to my model that dont exist in the database?
For example I have a calendar table, I want to retireve this data in my MVC controller then work out time left until each entry starts and return this to the view. So I would like another property in my calendar model to hold time left which is a value that I will generate outside of the database.
I've tried just adding the property but when I do that I get errors because the property is not mapped to anything.
Is this even possible?
Thanks
You should be able to add the property to the Model but you will not be able to query it with LINQ. LINQ will ultimately build and expression which it will want to run against the database using SQL. Its at that point that your LINQ will fail to find a mapping from your property to a field somewhere.
If your query returns an IEnumerable of the actual type on which you have created the property your view may be able to access it. I can't remember if EF insists on mapping in that case, it may do.
You might find that you can create subsequent LINQ query that uses LINQ-to-objects if you want to provide some other composite type to your view.
It's a non-persistent property or transient. I don't know Entity Framwork well but with a quick google search you should find the answer.
BTW you can find a lot of tips here :
http://weblogs.asp.net/zeeshanhirani/archive/2008/12/18/my-christmas-present-to-the-entity-framework-community.aspx
After making a quick search myself and a test in VS2008 I don't see a way to exclude a property from the mapping. Maybe it requires you to edit manually the mapping file ? :(

Resources