-1

I'm running a query against a MySQL database, and its statement is simply

SELECT * FROM tableOperationsHistory;

However, it fails and I get this exception (I translated it into English):

Fail activating restrictions. One or more rows contain values that violate non-null, unique or foreign-key restrictions.

This is the routine where the error happens:

Public Function RetrieveTable(ByVal command_text As String, Optional ByVal parameters As ParameterSet = Nothing) As DataTable
    Dim dt As New DataTable, retry = False
    Do
        Using comm = conn.CreateCommand
            comm.CommandText = command_text
            If parameters IsNot Nothing Then
                Dim par As DbParameter
                For Each pair In parameters
                    par = comm.CreateParameter
                    par.ParameterName = pair.Key
                    par.Value = pair.Value
                    comm.Parameters.Add(par)
                Next
            End If
            Dim rdr As DbDataReader
            RequestConnection()
            Try
                rdr = comm.ExecuteReader
                dt.Load(rdr)
                retry = False
            Catch ex As Exception
                retry = ShouldRetry(ex) 'EXCEPTION IS CAUGHT HERE
            End Try
            DismissConnection()
        End Using
    Loop While retry
    Return dt
End Function

And this is the table definition statement:

CREATE TABLE `tableOperationsHistory` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `numero` varchar(45) DEFAULT NULL,
  `pasta_id` int(11) DEFAULT NULL,
  `dataHoraInicioPrazo` datetime DEFAULT NULL,
  `dataHoraFinalPrazo` datetime DEFAULT NULL,
  `urgente` tinyint(4) DEFAULT NULL,
  `setorOrigem_id` int(11) DEFAULT NULL,
  `unidade_id` int(11) DEFAULT NULL,
  `setor_id` int(11) DEFAULT NULL,
  `usuario_id` int(11) DEFAULT NULL,
  `modalidadeComunicacaoJudicial_id` int(11) DEFAULT NULL,
  `modalidadeRepercussao_id` int(11) DEFAULT NULL,
  `especieTarefa_id` int(11) DEFAULT NULL,
  `postIt` varchar(255) DEFAULT NULL,
  `teor_observacao` varchar(255) DEFAULT NULL,
  `comunicacaoJudicial_id` int(11) DEFAULT NULL,
  `tarefa_id` int(11) DEFAULT NULL,
  `mensagens` text,
  `criadoPor_id` int(11) DEFAULT NULL,
  `criadoEm` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=92 DEFAULT CHARSET=latin1;

How can this happen in a SELECT statement? What should I do?

VBobCat
  • 2,527
  • 4
  • 29
  • 56

1 Answers1

0

This great community helped me to realize that such an error can be caused by "a mismatch in column definition (e.g. size of char fields) between the database and the dataset", and pointed me to another thread: https://stackoverflow.com/a/7029713/3718031

Possible relation with this, too: Column longtext exceeds the MaxLength limit

In my case, there are columns with "TEXT" datatype and it seems that might be the problem.

I thank deeply to all those who commented in my question and helped me to learn this.

EDIT: Since this question was voted-to-close and my problem remained, I tried to investigate a little further and made another question, I hope that one is properly exposed.

VBobCat
  • 2,527
  • 4
  • 29
  • 56