Search code examples
c#npgsqlcockroachdb

Npgsql has forgotten all data types


I'm connecting to unsecure local CockroachDb server using Npgsql, like this:

using (NpgsqlConnection connection = new NpgsqlConnection(@"Port=26257;Username=user;Database=thedata;Host=localhost")) {
  connection.Open();
  NpgsqlCommand command = new NpgsqlCommand("SELECT 1 AS records", connection);
  command.CommandType = CommandType.Text;

  using (NpgsqlDataReader reader = command.ExecuteReader()) {
    while (reader.Read()) {
      Console.WriteLine(reader["records"].GetType());
    }
  }
}

the GetType() fails with an exception:

The field 'records' has a type currently unknown to Npgsql (OID 20). You can retrieve it as a string by marking it as unknown, please see the FAQ.

If I follow the advice in the FAQ (AllResultTypesAreUnknown=true), I can get a string, but why not an int?

Yesterday, this was working fine for me, but I cannot figure out what has changed or become corrupted so that I cannot set or get any data types. I've removed and re-created the server, no luck. Re-installed the C# driver, no luck either. This is a reduced down code sample, the above happens in my 'real' code with parameterized values as well as retrieved results.


Solution

  • In an effort to secure the server, I had created a new user that had SELECT and INSERT privileges on the query'd table, but no privileges on the database. After re-walking through the CockroachDb C# primer, I realized that the example's user had full database privileges, and tried the same on a hunch. I'm guessing that the driver needs to access a schema table somewhere in order for it to figure out the query's data types (we'll ignore the fact that the sample I provided should not have needed that).

    GRANT SELECT ON DATABASE database TO user;