# Creating new PersistentRecord and validating unique constraints

**URL:** <https://forum.inductiveautomation.com/t/creating-new-persistentrecord-and-validating-unique-constraints/61038>\
**Category:** Module Development\
**Created:** [June 22, 2022, 1:00am UTC](https://forum.inductiveautomation.com/t/creating-new-persistentrecord-and-validating-unique-constraints/61038 "2022-06-22T01:00:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmusson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmusson/32/87530_2.png) [@bmusson](https://forum.inductiveautomation.com/u/bmusson)\
**Post date:** [June 22, 2022, 1:00am UTC](https://forum.inductiveautomation.com/t/creating-new-persistentrecord-and-validating-unique-constraints/61038/1 "2022-06-22T01:00:34Z")

</div>

I’m working on a driver-type module and would like to create a scripting function similar to `system.device.addDevice`. I have several different driver types, so I’m using the `ExtensionPoint` system.

Here’s what I’ve got so far - it seems to be working, but it’s letting me add multiple connections with the same name, which violates the unique constraint on the `BaseConnectionRecord.Name` field.

```auto
public void addConnection(String connectionType, String connectionName, PyDictionary connectionProps) {
    ExtensionPointManager epm = ConnectionExtensionPointManager.get();
    ExtensionPointType type = epm.getExtensionPoint(connectionType);
    if (type != null) {
  
        PersistentRecord baseRecord = context.getPersistenceInterface().createNew(BaseConnectionRecord.META);
        baseRecord.setString(BaseConnectionRecord.Name, connectionName);
        baseRecord.setString(BaseConnectionRecord.Type, connectionType);
        baseRecord.getMeta().getFieldMetas().forEach(fieldMeta -> {
            String fieldName = fieldMeta.getFieldName();
            if (connectionProps.containsKey(fieldName)) {
                baseRecord.setObject(fieldMeta, connectionProps.get(fieldName));
            }
        });
  
        PersistentRecord record = context.getPersistenceInterface().createNew(type.getSettingsRecordType());
        record.setReference(type.getSettingsRecordForeignKey(), baseRecord);
        record.getMeta().getFieldMetas().forEach(fieldMeta -> {
            String fieldName = fieldMeta.getFieldName();
            if (connectionProps.containsKey(fieldName)) {
                record.setObject(fieldMeta, connectionProps.get(fieldName));
            }
        });
  
        context.getPersistenceInterface().save(baseRecord);
        context.getPersistenceInterface().save(record);
    }
}

```

And in `BaseConnectionRecord.java`

```auto
  public static final IdentityField Id = new IdentityField(META);
  public static final StringField Name = new StringField(META, "Name", SFieldFlags.SMANDATORY, SFieldFlags.SDESCRIPTIVE);
  public static final StringField Type = new StringField(META, "Type", SFieldFlags.SMANDATORY, SFieldFlags.SDESCRIPTIVE);
  public static final StringField Description = new StringField(META, "Description", SFieldFlags.SDESCRIPTIVE);
  public static final BooleanField Enabled = new BooleanField(META, "Enabled", SFieldFlags.SDESCRIPTIVE).setDefault(true);

  static {
      Name.setUnique(true);
      Type.getFormMeta().setVisible(false);
  }

```

1. Is my approach of creating/linking to the base record correct?
2. What do I need to do to get this to respect the unique` BaseConnectionRecord.Name` constraint?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 22, 2022, 3:21pm UTC](https://forum.inductiveautomation.com/t/creating-new-persistentrecord-and-validating-unique-constraints/61038/2 "2022-06-22T15:21:28Z")

</div>

Did you add the unique constraint after you’d already generated your table in the .idb once? If you examine your config.idb in a SQLite viewer, does it actually have a unique constraint applied?

For complicated reasons, SQLite alterations to columns that already exist basically don’t get made, so the ‘Unique’ constraint might only be applied to editing via the web interface, and not actually applied at the persistence layer.

---

<div class="post-metadata">

**Author:** ![bmusson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmusson/32/87530_2.png) [@bmusson](https://forum.inductiveautomation.com/u/bmusson)\
**Post date:** [June 22, 2022, 11:08pm UTC](https://forum.inductiveautomation.com/t/creating-new-persistentrecord-and-validating-unique-constraints/61038/3 "2022-06-22T23:08:32Z")

</div>

> [@paul-griffith](#):
>
> Did you add the unique constraint after you’d already generated your table in the .idb once?

Yup, I did. In order to recreate the tables I uninstalled my module, shutdown the gateway, dropped all the tables, then reinstalled.

> [@paul-griffith](#):
>
> If you examine your config.idb in a SQLite viewer, does it actually have a unique constraint applied?

No, it doesn't. In fact, none of the default tables seem to have unique name constraints either.  
I'm running an old nightly, b2022032113.

My BaseConnectionRecord table:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/d/2da36f7382d9dc25c4b20e6a3654ec4151ae87a1.png)

Some default tables:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/7/978e7894def0ff3fbabec623c9d2b5ab598e1de2.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/1/118a419a06e038fb50791d3d266ba27ac7853451.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 27, 2022, 4:27pm UTC](https://forum.inductiveautomation.com/t/creating-new-persistentrecord-and-validating-unique-constraints/61038/4 "2022-06-27T16:27:13Z")

</div>

Huh, yeah, so it doesn’t. It looks like there’s an intention to apply the unique constraint to the table, but it’s not actually ever making it there.

With `system.device.addDevice`, we just do a manual check for duplicate names 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![bmusson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmusson/32/87530_2.png) [@bmusson](https://forum.inductiveautomation.com/u/bmusson)\
**Post date:** [June 27, 2022, 5:05pm UTC](https://forum.inductiveautomation.com/t/creating-new-persistentrecord-and-validating-unique-constraints/61038/5 "2022-06-27T17:05:33Z")

</div>

> [@paul-griffith](#):
>
> With `system.device.addDevice`, we just do a manual check for duplicate names 🤷‍♂️

Roger, that’s what I’ll do then.
