Compound database primary key - example doesn't work

Compound database primary key - example doesn't work

json81json81 Posts: 24Questions: 6Answers: 1

Hi!
I seems to me that the "Compound database primary key"-example doesn't work. I expected the error message to be "This staff member is already busy that day." but instead it shows the default SQL error message: "An SQL error occurred: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '32-2020-03-28' for key 'PRIMARY'"

https://editor.datatables.net/examples/advanced/compoundKey.html

/Anders

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    Yep, the example seems to be broken.

    For "lazy people" there is a way to take care of foreseeable SQL errors client side without writing a single line of server side code. This simply replaces the system error message with something more meaningful to the user.

    editor
    .on('postSubmit', function (e, json, data, action) {
      if (json.error) {
          if ( json.error.indexOf('1062 Duplicate entry') >= 0 ) {
              json.error = lang === 'de' ? 
                 "Leider darf ein Genehmiger nur einmal genehmigen. \n\
                  Bitte wählen Sie einen anderen Genehmiger!" : 
                 "Sorry, one approver may only approve once. \n\
                  Please choose a different approver!"; 
          }
          if ( json.error.indexOf('1452 Cannot add or update a child row') >= 0 ) {
              json.error = lang === 'de' ? 
                  "Keine Dateneingabe erlaubt, nur Auswahl!" : 
                  "Data entry not permitted, only selection!";
          }
      }
    })
    
  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    Thanks for pointing that out, and yep, I'm seeing that too. I've raised it internally (DD-1393 for my reference) and we'll report back here when there's an update (it'll probably be on the next web-site build which tends to be around the time of releases).

    Cheers,

    Colin

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    I missed the validator for it unfortunately. There should be a global validator for the instance which checks the database to see if there is already a matching item in the database, and if so, then return a nice looking error rather than the SQL one. Its just a case of catching the error earlier.

    @json81 - What server-side platform are you using?

    Allan

  • json81json81 Posts: 24Questions: 6Answers: 1

    Thanks for all replies. I am using PHP.

    Best Regards
    Anders

  • paul@dmcreative.compaul@dmcreative.com Posts: 27Questions: 5Answers: 0

    I see this error is still occurring in the online examples, have the php libraries been updated in Editor 2.0 to return a cleaner error message?

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    edited April 2021

    Right enough I still haven’t written the validator for it. It isn’t the libraries that need to be updated, but rather a validator used for in the example PHP. I’ve got a bug for this to be fixed in the next patch release now.

    Allan

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    That's the change finally made for this and it will drop in 2.0.2. The compound key example does actually have an edit validator already, but not a create one, which is what I have now added.

    Regards,
    Allan

This discussion has been closed.