Python API do not follow schema

Hello:
I’m trying to use a bucket schema to restrict the writing to the bucket.
Using the InfluxDB v2 API I have created the schema showed below:

[
  {
    "links": {
      "self": "/api/v2/buckets/a7d9eb85c8c223b8/schema/measurements/0aef4bcd49e56000"
    },
    "id": "0aef4bcd49e56000",
    "orgID": "XXXXX",
    "bucketID": "a7d9eb85c8c223b8",
    "name": "SmartMeter",
    "columns": [
      {
        "name": "time",
        "type": "timestamp"
      },
      {
        "name": "iddispositivo",
        "type": "tag"
      },
      {
        "name": "pactiva",
        "type": "field",
        "dataType": "float"
      },
      {
        "name": "preactiva",
        "type": "field",
        "dataType": "float"
      },
      {
        "name": "creation",
        "type": "field",
        "dataType": "float"
      }
    ],
    "createdAt": "2023-03-23T06:39:41Z",
    "updatedAt": "2023-03-23T06:39:41Z"
  }
]

When I write the next two data points using the Python client library, both are correct:

    data = [{
        "measurement": "SmartMeter3",
        "tags": {"iddispositivo": "id001"},
        "fields": {"pactiva": 38.5, "preactiva": 54.5, "creation": time.time()},
        "time": time.time_ns() 
        },
        {
            "measurement": "SmartMeter",
            "tags": {"iddispositivo": "id002"},
            "fields": {"pactiva": 36.5, "preactiva": 'two', "creation": time.time()},
            "time": time.time_ns() 
        },
    ]

As far as I understood in the documentation, the first line should not be allowed because only “Smarmeter” measurement is defined. In the second case “Smartmeter” should not allow to insert “preactiva” as string.
When I write the two data point using the InfluxDB CLI, the first data point is also allowed but not the second one. In this case I get the error:
failed to write data: 400 Bad Request: failed to parse line protocol: error parsing line 1 (1-based): Could not parse entire line. Found trailing content: ‘preactiva=two,creation=56.54’

Any help to understand how to restrict the fields datatype using the schema and the python client library? And how to restrict new measurements not included in the schema?

Thank you

Hello @Roberto_Arnanz,
Yes that fails because you’re trying to mix types for the same series.
Field data types are restricted by default for the same series.
You cant restrict the types with the python client library.

It looks like you’re already correctly restricting types with the client library.

I’m sorry I must be dense. Everything looks like its behaving as expected to me. What am I missing?

Hello @Anaisdg:

Thank you for your reply.

My question is related to the use of schemas.

If you look at the example in the first data point I write in measurent “SmartMeter3” that is not defined in the schema, but it insert the data both with InfluxDB CLI and Python client library. What I understood in the documentation is than only measurements defined in the schema are allowed.

In the second case, related to fields with different types the problem that I found is that when I use the Python client library it allows to write a string in “preactiva”. As you mention the Influx DB CLI behaviour is correct, but not for the Python library

So my question is double:

  • Is it possible to restrict the inserted measurements to the ones defined in the schemas?
  • Could it be possible that Python library has a bug and is not checking properly the schemas befor inserting the data?

If it helps I can share the code I’m using in this example

Thank you