# Gladys V4 - Affichage de capteurs dans l'interface

**URL:** <https://community.gladysassistant.com/t/gladys-v4-affichage-de-capteurs-dans-linterface/4939>\
**Category:** Présentations\
**Created:** [Septembre 23, 2019, 7:32 UTC](https://community.gladysassistant.com/t/gladys-v4-affichage-de-capteurs-dans-linterface/4939 "2019-09-23T07:32:14Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![pierre-gilles](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/pierre-gilles/32/5886_2.png) [@pierre-gilles](https://community.gladysassistant.com/u/pierre-gilles)\
**Post date:** [Septembre 30, 2019, 9:09 UTC](https://community.gladysassistant.com/t/gladys-v4-affichage-de-capteurs-dans-linterface/4939/11 "2019-09-30T09:09:31Z")

</div>

@bertrandda malheureusement ta solution ne fonctionne pas à 100%, en fait tu as mis le doigt sur un bug (ou une feature^^) de sequelize!

En SQlite ALTER TABLE n’existe pas. La seule façon de mettre à jour une table est de la renommer, de la recréer et de migrer les données.

Sequelize a abstrait cette partie, et fait ça quand tu fais ton changeColumn:

```auto
Executing (default): PRAGMA TABLE_INFO(`t_device`);
Executing (default): CREATE TABLE IF NOT EXISTS `t_device_backup` (`id` UUID NOT NULL PRIMARY KEY, `service_id` UUID NOT NULL, `room_id` UUID, `name` VARCHAR(255) NOT NULL, `selector` VARCHAR(255) NOT NULL, `model` VARCHAR(255), `external_id` VARCHAR(255) NOT NULL, `should_poll` TINYINT(1) NOT NULL DEFAULT 0, `poll_frequency` INTEGER, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL);
Executing (default): INSERT INTO `t_device_backup` SELECT `id`, `service_id`, `room_id`, `name`, `selector`, `model`, `external_id`, `should_poll`, `poll_frequency`, `created_at`, `updated_at` FROM `t_device`;
Executing (default): DROP TABLE `t_device`;
Executing (default): CREATE TABLE IF NOT EXISTS `t_device` (`id` UUID NOT NULL PRIMARY KEY, `service_id` UUID NOT NULL, `room_id` UUID, `name` VARCHAR(255) NOT NULL, `selector` VARCHAR(255) NOT NULL, `model` VARCHAR(255), `external_id` VARCHAR(255) NOT NULL, `should_poll` TINYINT(1) NOT NULL DEFAULT 0, `poll_frequency` INTEGER, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL);
Executing (default): INSERT INTO `t_device` SELECT `id`, `service_id`, `room_id`, `name`, `selector`, `model`, `external_id`, `should_poll`, `poll_frequency`, `created_at`, `updated_at` FROM `t_device_backup`;
Executing (default): DROP TABLE `t_device_backup`;

```

Or, si tu lis bien le code, il y a un bug dans cette implémentation: les foreign key + les contraints ne sont pas migrées. Donc oui, ton name n’a plus de contraintes: mais toutes les autres columns ont aussi perdu leurs contraintes aussi, et la table se retrouve sans aucunes protections. Le selector n’est plus unique, les foreign keys ont sautés, etc…

Cf code :

> <https://github.com/sequelize/sequelize/blob/efd2f40620107cef01e56856457ba1b607af79f5/lib/dialects/sqlite/query-interface.js#L46>

Après fouille du GitHub de Sequelize, il y a une issue créé en 2017 qui référence le problème:

> <https://github.com/sequelize/sequelize/issues/7078>
>
> \## What you are doing?
> I'm trying to rename a column on an Sqlite db. I use \`re…nameColumn\` on the \`QueryInterface\` that, in the Sqlite case, is going to call \`renameColumn\` on the \`SQLiteQueryInterface\`.
> 
> \## What do you expect to happen?
> I expect that:
> 1) a temporary table is created equal to the first one (except the renamed column) including foreign key references.
> 2) data is copied over to the temporary table
> 3) old table is deleted
> 4) a new table is created equal to the first one (except the renamed column) including foreign key references.
> 5) data is copied over to the new table
> 
> \## What is actually happening?
> 1) a temporary table is created equal to the first one (except the renamed column) \*\*NOT including foreign key references\*\*.
> 2) data is copied over to the temporary table
> 3) old table is deleted
> 4) a new table is created equal to the first one (except the renamed column) \*\*NOT including foreign key references\*\*.
> 5) data is copied over to the new table
> 
> This is the original table:
> \`\`\`SQL
> CREATE TABLE \`matches\` (\`id\` INTEGER PRIMARY KEY AUTOINCREMENT, \`hash\` VARCHAR(200) NOT NULL UNIQUE, \`alertId\` INTEGER REFERENCES \`alerts\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE)
> \`\`\`
> 
> This is the table after the renameColumn on 'hash' -\> 'foo'
> \`\`\`SQL
> CREATE TABLE \`matches\` (\`id\` INTEGER PRIMARY KEY AUTOINCREMENT, \`foo\` VARCHAR(200) NOT NULL, \`alertId\` INTEGER)
> \`\`\`
> 
> \_\_Dialect:\_\_ sqlite
> \_\_Database version:\_\_ 3.9.x
> \_\_Sequelize version:\_\_ 3.24.x

Mais visiblement le bug n’est toujours pas résolu…

**Trois options:**

- On utilise un “hack” qui considère à override une des fonctions de Sequelize lors de cette migration pour re-rajouter les contraintes.
- On trouve un moyen de fixer Sequelize et on propose une PR (après pas sur que ça soit mergé avant qu’on en ait besoin)
- On oublie de faire un changeColumn et on demande aux gens de re-créer la DB… ce qui fixe à court terme, mais bon si à long terme on veut changer une column le problème est toujours là !

---

_[View the full topic](https://community.gladysassistant.com/t/gladys-v4-affichage-de-capteurs-dans-linterface/4939)._
