Not able to add datasource: error="UNIQUE constraint failed: data_source.org_id, data_source.uid"`

I am not able to add a data source in Grafana any more from the front end as well from the backend i.e via HTTP API.

Only happening on one instance not on all instances of Grafana.

A few days ago I was able to add data sources of different types like Prometheus, and AWS CloudWatch but not anymore.

I didn’t update the Grafana and the version is v6.6.0 (5c11bbdfb4).

From FrontEnd whenever I’m clicking any source type I am getting an error saying “Failed to add datasource” even though I am admin.


Happening for all type of Datasources like Prometheus, Graphite, CloudWatch

This is the error message in the log file: t=2020-06-17T14:47:30+0000 lvl=eror msg="Failed to add datasource" logger=context userId=1 orgId=1 uname=admin error="UNIQUE constraint failed: data_source.org_id, data_source.uid"

But it’s not letting me even select the name to break any constraints.

I checked my database also.
sqlite> select * from data_source;
12|1|5|prometheus|Thanos|proxy|http://localhost:9091||||0|||0|{}|2020-06-02 20:54:23|2020-06-12 15:36:05|0|{}|0|0

If I am trying to send via HTTP API same message is getting logged.

1 Like

Actually, I have upgraded recently from v6.6.0 to v7.0.1 and then downgraded back to v6.6.0 so that’s why I guess some database schema is changed.

I removed the grafana.db, which I was using and regenerated a new one by simply restarting grafana and then able to able datasource easily.

1 Like

Instead of deleting the whole grafana.db file, you can also open it with a tool like https://sqlitebrowser.org/. There, you can delete just the one UNIQUE constraint (on table data_source), then save the db file and restart Grafana. This worked for me (I also downgraded from Grafana 7 to 6.7.4 before the error occurred first).

1 Like

Thanks @mgrundkoetter for replying,

I rolled back to the previous grafana version and took a new db since now much data was present, it worked fine. I think while upgrading and downgrading caused the issue.

Yes, will try the approach with dummy data as told by you.

Thanks!

Hi @mgrundkoetter,

By using above workaround after rollback we are able to add datasource but it leads to one more issue i.e mentioned below.
Workaround with rollback worked, later we upgraded the grafana chart from 6.6.2 to 7.1.3 we observed that UQE_data_source_org_id_uid unique index was not added back.
If index UQE_data_source_org_id_uid is not added back, will it cause any effects on grafana functionality.

Hi @mgrundkoetter

By using above workaround after rollback we are able to add datasource but it leads to one more issue i.e mentioned below.
Workaround with rollback worked, later we upgraded the grafana chart from 6.6.2 to 7.1.3 we observed that UQE_data_source_org_id_uid unique index was not added back.
If index UQE_data_source_org_id_uid is not added back, will it cause any effects on grafana functionality.