Description:
Problem started in version 8.0.22 and still exist in version 8.0.23 of the NET connector.
If the column is a CHAR(36) and has a NULL value on it the connector throws the exception:
Error: System.ArgumentOutOfRangeException: Non-negative number required. (Parameter 'count')
at System.Text.EncodingNLS.GetString(Byte[] bytes, Int32 index, Int32 count)
at MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(Int32 index, MySqlField field, IMySqlValue valObject)
This is an error because in the case of Foreign Keys the column should be allowed to have null values.
It's possible that this problem is still more wide because any column of type CHAR(36) is considered a GUID column including when no values other than NULL exist on it.
The only cases were no exception is generated for a column CHAR(36) are:
- the table is empty
- the table has only valid guid values
How to repeat:
The simple process to repeat the problem is to create a table representing the field and add a null value to it.
CREATE TABLE guid_test(
guid CHAR(36)
);
INSERT INTO guid_test VALUES (NULL);
Then we run a simple test (C# query example):
string query = "SELECT * FROM guid_test;";
MySqlDataAdapter da = new MySqlDataAdapter(query, conn);
DataSet ds = new DataSet();
da.Fill(ds, "guid_test");
DataTable dt = ds.Tables["guid_test"];
And it will fail with the error:
System.ArgumentOutOfRangeException: Non-negative number required. (Parameter 'count')
if we insert an invalid, not null value it will fail with another exception.
INSERT INTO guid_test VALUES ('abcd1234');
Error: System.ArgumentException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).Couldn't store <abcd1234> in guid Column. Expected type is Guid.
If the table only has valid values it works as expected:
INSERT INTO guid_test VALUES ('462fd12d-002f-4000-a57e-3cfa0bba6f01');
Suggested fix:
Because, by definition, FOREIGN KEY allows inserting NULL values if there is no NOT NULL constraint defined on this key, at least the parsing of values for GUID should allow the NULL value on it.
If a CHAR(36) column will always be considered a GUID column then some warning should be added to the documentation or to the release notes, because any other value will throw exceptions.