IMD SolutionComputing LibraryShop main store

Troubleshooting

Why wideTo writes _field instead of your MySQL datapoint ID

Edge computing and IIoT Published 6 min read By IMD Solution engineering team

Short answer

wideTo() treats every ordinary column as a field named after that column, so the aliased _field column lands in the bucket as a field literally called _field. Keep the rows narrow — dataPointId as _field, pointValue as _value, FROM_UNIXTIME(ts/1000) as _time — and write them with to(). Verify the epoch is milliseconds against one known row before scheduling the task.

On this page
  1. Why does Explore show _field for every row instead of the datapoint ID?
  2. Why can't wideTo() read the field key from the _field column value?
  3. Why did every point land at the task run time instead of the MySQL timestamp?
  4. How do I check that the epoch is milliseconds before converting it?
  5. Which query writes dataPointId as the field key with its source timestamp?
  6. How do I prove the import writes correctly before the full run?
  7. What goes wrong when the MySQL import task reruns with a row limit?
  8. Should the import keep wideTo() or move to to()?
  9. Questions asked

Your task runs green and Explore still lists _field as the field key of every row because wideTo() names fields after columns: it never turns a column value into a field key. The same task overwrites the converted MySQL timestamp with now(), so every row in a run lands at the execution instant. Keep the rows narrow — dataPointId aliased to _field, pointValue to _value, FROM_UNIXTIME(ts/1000) to _time — map only _measurement, and write with to() instead of influxdb.wideTo(). Verify against one known MySQL row that ts really is milliseconds before you schedule anything.

Why does Explore show _field for every row instead of the datapoint ID? #

wideTo() builds each field from a column name, so the column you aliased to _field is stored as one field whose key is the literal text _field, with your datapoint IDs sitting in its values.

The function expects a wide table: one row per series, one column per field, values aligned on _time. Apart from _time and _measurement, every column of its input becomes a field named after that column. Your result table therefore grows two fields: one called _field, holding datapoint IDs, and one called _value, holding the sensor readings, while no field ever takes a datapoint ID as its key. That is exactly what Explore shows: a field list where _field sits where a real ID such as Temp01 should be.

Why can't wideTo() read the field key from the _field column value? #

wideTo() mirrors schema.fieldsAsCols() on the write side: it takes a table with one column per field and stores each column as a field, so a column named _field is just another field source and its name, not its cell values, decides the key.

The two Flux shapes behave differently. A narrow row carries exactly one _measurement, _time, _field and _value, and to() is built to write that shape directly. A wide row carries one column per field with the values aligned on _time, and that is the table wideTo() is for. schema.fieldsAsCols() pivots narrow query results on _field and _time so fields sit side by side in a chart; pivoting first would only rebuild the wide shape wideTo() expects, and the narrow table needs no transformation, because to() reads _field straight from the column. Choose wideTo() only for results that already have one column per field; choose to() for row-shaped results like this import.

Why did every point land at the task run time instead of the MySQL timestamp? #

The map step assigns _time: now(), so each run stamps every row with the execution instant and discards the FROM_UNIXTIME result; the imported points carry no source time at all.

With one measurement, no tags and a single shared timestamp, all rows of a run collapse into one series where each field key holds exactly one value per timestamp, and the last write wins. A whole import run therefore survives as one datapoint ID and one reading. Feeds that stamp duplicate timestamps hit the same rule for a different reason, described in the article on why Telegraf writes one InfluxDB point from 80 MQTT entries. Drop the _time assignment from the map step, keep only _measurement, and the aliased _time column flows through to the write unchanged.

How do I check that the epoch is milliseconds before converting it? #

FROM_UNIXTIME() takes seconds, so ts/1000 is correct only when ts holds milliseconds; one known row settles it — for example 1699999999000 becomes 1699999999 seconds, which FROM_UNIXTIME renders as 2023-11-14 22:13:19.

The arithmetic: 1699999999000 ms ÷ 1000 = 1699999999 s, and epoch second 1699999999 is 2023-11-14 22:13:19 UTC, so a row you know was logged that afternoon confirms the unit. If the column already held seconds, the same division gives 1699999.999, which FROM_UNIXTIME renders in January 1970 — timestamps stuck in 1970 are the signature of a seconds source divided once more. FROM_UNIXTIME also displays in the server session's time zone, so a whole-hour offset against the known instant points at the session zone rather than at the units. The same value-against-date check for epoch-millisecond tags, with worked conversions, is the subject of the guide on calculating epoch milliseconds from date tags.

Which query writes dataPointId as the field key with its source timestamp? #

Keep the SQL aliases, map only the measurement, and call to() — the narrow writer reads _field, _value and _time straight from the columns and stores the converted timestamp unchanged.

The address, login and password in the data source name below are example values for a lab server; put the MySQL account this task is meant to run under in their place.

import "sql"
import "influxdata/influxdb"

option task = {name: "sql_query", every: 10m}

sql.from(
    driverName: "mysql",
    dataSourceName: "scadareader:examplePassword@tcp(192.168.1.50:3306)/scada",
    query: "SELECT FROM_UNIXTIME(ts/1000) AS _time, dataPointId AS _field, pointValue AS _value FROM pointValues LIMIT 100000",
)
    |> map(fn: (r) => ({r with _measurement: "MyMeasurement"}))
    |> to(bucket: "scada")

MySQL accepts the aliases quoted or plain; what matters is that the columns reach Flux named _time, _field and _value. If the task rejects the write, check the to() signature that ships with your Flux release — organization, host and token are the arguments some runtimes want explicitly, and they come from the task's own configuration.

Run as written, this task stores each datapoint ID as its own field key with the converted source timestamp.

How do I prove the import writes correctly before the full run? #

Run once against a small, known slice — shrink the row limit to a handful of rows you can name in MySQL — then read the points back and compare field key, value and timestamp to that row before scheduling.

Check these against the source rows you picked:

  • Explore lists one field per datapoint ID and no field literally named _field.
  • Each field's values equal pointValue for the row carrying that ID.
  • Each point's timestamp equals the FROM_UNIXTIME output for its row, not the task's run time.
  • Two datapoint IDs sharing one source time appear as two fields on one point, not as two competing writes.

One caution before scaling up: if this bucket feeds anything beyond trend charts — alarms, batch records, compliance reports — confirm the backfilled timestamps against known process events, because a backfill with wrong times silently rewrites reported history. And if Explore stays empty rather than showing wrong fields, the fault is delivery, not mapping; that is the separate failure mode behind why InfluxDB asynchronous writes never land in the bucket.

What goes wrong when the MySQL import task reruns with a row limit? #

The query has no time filter and no ordering, so every run re-reads the same oldest 100,000 rows and rewrites them, while anything past the limit is never imported at all.

Rewriting identical rows only wastes a run; rewriting rows whose source values have changed edits history without a trace. Give the selection a watermark instead: record the largest ts already imported, add a WHERE clause on ts above that value in the same epoch unit as the column, order ascending, and remove the fixed limit once the watermark is proven. Comparing and filtering history on epoch-millisecond timestamps, boundary cases included, is what the guide on filtering history with epoch milliseconds covers.

Should the import keep wideTo() or move to to()? #

Move this import to to(): the rows are narrow, one datapoint ID and one value each, and to() is the writer built for that shape, while wideTo() would keep naming the field _field no matter how the query is aliased.

Keep wideTo() only for results that already carry one column per field, where the column names are the field keys you want. The switch leaves one cleanup step: the miswritten series stays in the bucket until deleted, so Explore keeps listing the literal _field beside the real datapoint IDs until that series is gone; the deletion steps are in the FAQ below.

Decision: switch this task to to(), map only _measurement, keep FROM_UNIXTIME's output in _time, prove one known row end to end, then add the ts watermark and put the task on its schedule.

Questions asked

Why do imported pointValues plot as text instead of numbers?

If the MySQL column pointValue is a character type, every imported _value is a string field, and string fields plot as category text with no math. InfluxDB fixes a field key's value type at the first write into a shard, so later writes of the other type are rejected. Cast the column in SQL, for example CAST(pointValue AS DECIMAL(12,3)), and remove the string-typed series before rerunning.

Which columns become tags when to() writes the rows?

to() writes _time, _measurement, _field and _value as their own roles and turns every other column in the row into a tag. Add a line or asset column, for example, and identical rows from two sources keep separate points even at the same timestamp, because the tag values differ; without a distinguishing tag, the later write overwrites the earlier one.

Does this task run on any InfluxDB release?

No. Flux tasks using sql.from and to() run on the InfluxDB 2.x line; the 3.x releases do not run Flux, so confirm the server version before porting the task. Match the function signatures to the Flux version your server ships, because parameters such as the organization or token arguments to to() differ between releases.

How do I remove the literal _field series the old task wrote?

Delete the series by predicate before rerunning, or Explore keeps listing the literal field. The bucket's delete interface accepts a predicate, so a filter on the measurement equal to MyMeasurement and the field key equal to the literal _field removes only the miswritten points. Afterwards, query for that field key and confirm nothing returns.

Keep reading

  • Troubleshooting

    Why a renamed InfluxDB field writes after a field type conflict

    InfluxDB binds each field key to one data type per shard, and delete removes points, not that type record, so an emptied field still refuses a float. A renamed field key starts with a new type, which is why your renamed fields write. Keep the new key and convert the old strings with toFloat() in Flux instead of deleting again.

    Edge computing and IIoT

  • How-to

    Configuring OPC UA to MySQL writes with a Template node

    A Template node between the OPC UA client and the MySQL node renders the counter into an INSERT statement on the topic property, the place the standard MySQL node looks for its query. The payload must carry only the scalar value, and the Now function stamps database execution time, not the OPC UA source timestamp.

    Edge computing and IIoT

  • How-to

    Filter MQTT fields in Telegraf before they reach InfluxDB

    Yes: add fielddrop to the mqtt_consumer input block in telegraf.conf and list the parsed field names, for example ENERGY_ApparentPower or the suffix glob *_ReactivePower. The filter runs at the end of the input, so it shapes new points only and never rewrites points already stored in InfluxDB. Drop a minority of fields with fielddrop, keep a minority with fieldinclude, and filter tags with tagdrop.

    Edge computing and IIoT

Why a report table shows 1 instead of historian values