How to configure components for a master detail MYSQL structure - mysql

I have two database tables that one is the master one and the other one the Detail table.
I use dbexpress SQLQuery component for Master table, a Datasetprovider & a clientdataset. I checked Autorefresh, Cascade Deletes, Cascade Updates for the Datasetprovider of the Master Table. I also set on the updateData event of the Datasetprovider all the fields of the Master Table with the flag "pfInUpdate"
apart from the key field that I also set "pfInKey" & "pfInWhere" flags.
SQLQuery1.Active:=true;
SQLQuery1.sql.Clear;
SQLQuery1.SQL.Add('select table1.*,');
SQLQuery1.SQL.Add('table2.AKRO_B_ID,table2.AKRO_B_EETT,table2.AKRO_B,
...');
SQLQuery1.SQL.Add(' from PATH1 as table1');
SQLQuery1.SQL.Add('left join PATH2 as table2 ON
(table1.LINK_ID = table2.LINK_ID)');
ClientDataset1.Active:=True;
ClientDataset1.Open;
ClientDataset1.LogChanges:=True;
Datasource1.Enabled:=True;
I use dbexpress SQLDataset component for Detail table, a Datasetprovider & a clientdataset.
I use the following syntax on the Create Event of the main form for Detail Table Components:
SQLDataSet1.CommandText:='SELECT * FROM PATH2 WHERE LINK_ID=LINK_ID';
ClientDataset2.Mastersource:=DataSource1;
ClientDataset2.MasterFields:='LINK_ID';
ClientDataset2.IndexFieldNames:='LINK_ID';
SQLDataSet1.Active:=TRUE;
SQLDataSet1.Open;
ClientDataset2.Active:=True;
ClientDataset2.Open;
ClientDataset2.LogChanges:=True;
Datasource2.Enabled:=true;
The problem is that when I made a change on the Master Table the Post event works fine but when I made a change on the fields of the Detail table that does not work and the detail table remains unchanged. It seems also that the insert command doesn't have any effect on the two Database tables. Hence what is the right way to configure all the components in order the master detail structure to work fine?

Related

trigger update copy information

I want to create a trigger that does the following:
copy one column of info jobnumber from one table (jobs) to another (materials) on an existing record to attachedjobnumber.
I haven't found the correct syntax to say this. when I insert a new job - nothing gets update and no new row is inserted,,, but there are no error messages in the logs.
I also need to set the bool (hasjobnumber) equal to true - when I test that trigger - it works fine.
which makes me think that setting the value of material.attachedjobnumber = jobs.jobnumber is the problem, my guess is that jobs.jobnumber isn't in reference when updating table material...
if that's true - what's the proper syntax for this?
I've tested separate triggers, and so far this trigger works fine.
UPDATE material
SET isjobyet = "HAS"
WHERE barcode1 IN (
SELECT primaryRFID
FROM jobs
WHERE jobs.primaryRFID = material.barcode1
)
since this code does work - I make the assumption that the non-static JobNumber value is the source of the problem. since "HAS" is correctly updated.
UPDATE material
SET material.AttachedJobNumber = jobs.JobNumber
WHERE barcode1 IN (
SELECT primaryRFID
FROM jobs
WHERE jobs.primaryRFID = material.barcode1
)
from this - I expect that after each inserts on the table jobs:
jobs.JobNumber be assigned to the material.AttachedJobName
this updates only the material row where the material.barcode1 =jobs.primaryrfid.
but no new row is inserted at all.
Before you perform UPDATE,
Actually you can use the same script using SELECT [skip the UPDATE SYNTAX]
That way you can monitor your script without committing anything yet.
And also I dont recommend using this inside the subquery
WHERE jobs.primaryRFID = material.barcode1
This condition connecting a table works on IN-SELECT subquery.
If you are performing subqueries inside the [WHERE] clause, try to treat it as different buffer, dont connect them first.

Integration custom tables with pas.plugin.sqlalchemy

I'm trying to integrate custom table for authentication and role into PAS.plugin.sqlalchemy.
After customisation model.py I did manage to get my user table schema as we have in existing user table. But while installation of this pas, its create numbers tables inculding property table. Property table linked with user table through foreign key. I don't have any property table in existing scenario.
By seeing my trackback I can see while doing SQL fetch pas looks for users and property table as join query. we have data in our user (web_contact_auth) table but not in property table.
u'SELECT TOP 1 web_contact_auth.contact_number AS web
_contact_auth_contact_number, web_contact_auth.password AS web_contact_auth_pass
word, principals.id AS principals_id, principals.type AS principals_type, princi
pals.zope_id AS principals_zope_id, web_contact_auth.password_reset_token AS web
_contact_auth_password_reset_token, web_contact_auth.password_reset_expiry AS we
b_contact_auth_password_reset_expiry \nFROM principals JOIN web_contact_auth ON
principals.id = web_contact_auth.contact_number \nWHERE web_contact_auth.contact
_number = ?' ('admin',)
My questiones are:
how should I create data in property table ? Is there anyway that I can ignore property table and just fetch data from user table only.
Adavance Thanks your time for reading/reply.
Regards
WEBBYFOX
Just don't activate that plugin. I don't have a properties table. In /acl_users/plugins/manage_active, Properties Plugins, I have sql as the last option. I don't see anything else obvious.

Define custom POST method for MyDAC

I have three tables objects, (primary key object_ID) flags (primary key flag_ID) and object_flags (cross-tabel between objects and flags with some extra info).
I have a query returning all flags, and a one or zero if a given object has a certain flag:
SELECT
f.*,
of.*,
of.objectID IS NOT NULL AS object_has_flag,
FROM
flags f
LEFT JOIN object_flags of
ON (f.flag_ID = of.flag_ID) AND (of.object_ID = :objectID);
In the application (which is written in Delphi), all rows are loaded in a component. The user can assign flags by clicking check boxes in a table, modifying the data.
Suppose one line is edited. Depending on the value of object_has_flag, the following things have to be done:
If object_has_flag was true and still is true, an UPDATE should be done on the relevant row in objects_flags.
If object_has_flag was false but is now true, and INSERT should be done
If object_has_flag was true, but is now false, the row should be deleted
It seems that this cannot be done in one query https://stackoverflow.com/questions/7927114/conditional-replace-or-delete-in-one-query.
I'm using MyDAC's TMyQuery as a dataset. I have written separate code that executes the necessary queries to save changes to a row, but how do I couple this to the dataset? What event handler should I use, and how do I tell the TMyQuery that it should refresh instead of post?
EDIT: apparently, it is not completely clear what the problem is. The standard UpdateSQL, DeleteSQL and InsertSQL cannot be used because sometimes after editing a line (not deleting it or inserting a line), an INSERT or DELETE has to be done.
The short answer is, to paraphrase your answer here:
Look up the documentation for "Updating Data with MyDAC Dataset Components" (as of MyDAC 5.80).
Every TCustomDADataSet (such as TMyQuery) descendant has the capability to set update SQL statements using SQLInsert, SQLUpdate and SQLDelete properties.
TMyUpdateSQL is also a promising component for custom update operations.
It seems that the easiest way is to use the BeforePost event, and determine what has to be done using the OldValue and NewValue properties of several fields.

MS Access: Auto update fields in same table

Sorry but I'm not very experienced when it comes to things like this.
I have a table called "Measure Table" with the fields "ID", "Proxy" and "ProxyID".
I created a form based on this table. ID is a label pre-populated from the table. Proxy is a drop down menu with the options "For" or "From". ProxyID contains a drop down with the same numbers as ID.
I would like a user to go to a specific record in the form (say for ID:I800), select "For" from the Proxy drop down and then select ProxyID (lets say L800). For the record for L800, I want it to automatically change the proxy to "From" and the ProxyID to I800.
Is this possible in Access?
Thanks so much for any help provided
Here is a visual of what i wnat to happen:
I want the table to look like this before the update(when the user selects "For" and "L800"):
Record# ID Proxy ProxyID
1 I800 For L800
2 L800
Then the table is automaticaly updated to:
Record# ID Proxy ProxyID
1 I800 For L800
2 L800 From I800
Okay, here is the gist of what you need to do to solve your immediate problem (updating the corresponding row in the other table.
Simply add an event handler to the AfterUpdate event of the form to perform the update to the other row. The code should look very similar to this...
Private Sub Form_AfterUpdate()
Dim RelatedID As String
Dim Proxy As String
If (UCase(Me.Form!Proxy) = "FOR") Then
RelatedID = Me.Form!ProxyID
CurrentID = Me.Form!ID
DoCmd.RunSQL ("UPDATE [Measure Table] SET ProxyID='" & CurrentID & "', Proxy='From' WHERE ID='" & RelatedID & "'")
End If
End Sub
Caveats:
As I mentioned in the comments, this data structure is a very bad idea and will create a lot of extra work for you to maintain the data integrity according to the implicit rules you are specifying as a matter of course with this design. I realize you have an existing DB to deal with, but frankly it would probably be less work to fix the DB design than maintain this one in the long run.
Some additional considerations you didn't ask about, but are going to need to deal with:
What happens if someone updates
either of the entries in a pair
directly in the table instead of
using your form? There really isn't a
good way to apply the above logic to run when
except in the context of using the form.
What happens in this code if the related row doesn't exist for some reason?
What happens if the related row "The FROM" row is updated in the form?
What happens if either row is deleted from the table?

Does LINQ to SQL support Single Unit of work only or can I persist the Context and do Next operation?

I have a windows application. I am trying to insert a record through a DataContext. It has Unique identifier in the table. Even I am executing a trigger after insertion. So I am making a select query in the end of the trigger to get the auto generator number and to avoid auto-sync error. As it's a windows application I can keep the Context for longtime. When I create a new object ( for example order) and do the same previous operation, upon SubmitChanges operation, it shows cannot have duplicate key. Why can't I use this same Context to Insert the second record? Or do I need to create a new Context to insert a new Record?(Does this Unit of work Concept comes here?). Creating new Context is bad idea as I need to load all data again..
Any thought?
Some code sample to explain my situation:
CallCenterLogObjCotext = (CallCenterLogObjCotext == null ? (new CallcenterLogContext) : (CallCenterLogObjCotext));
CallDetail newCallDetailsOpenTicket = new CallDetail();
newCallDetailsOpenTicket.CallPurpose = (from callpuposelist in CallCenterLogObjCotext.CallPurposes
where callpuposelist.CallPurposeID == ((CallPurpose)(cbcallpurpose.SelectedItem)).CallPurposeID
select callpuposelist).FirstOrDefault();
Lots of settings like this ...
CallCenterLogObjCotext.CallDetails.InsertOnSubmit(newCallDetailsOpenTicket);
CallCenterLogObjCotext.SubmitChanges();
As I mentioned above, this is a click on Open Ticket button on windows form. I change the values of fname, lname and all in the textboxes available on that form and clicked the same button.
So it will call the same method again. I get the below specified error:
System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key that is already in use.
You can insert more than one row with the same context object, see http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx, http://msdn.microsoft.com/en-us/library/bb425822.aspx, and other numerous online examples. The duplicate key issue could be a linq to sql configuration issue, or a database integrity error, i.e. such as if you have a natural primary key on a table and are trying to insert a row with the same natural primary key more than once.