Skip to content

Support for UNIQUE #12

@coderbuzz

Description

@coderbuzz

Hi,

I propose UNIQUE support, SQLite will automatically create INDEX.

SQLite3.prototype.propertySettingsSQL = function (model, prop) {
    var p = this._models[model].properties[prop];

    return datatype(p) + 
    //// In case in the future support user defined PK, un-comment the following:
    // (p.primaryKey === true ? ' PRIMARY KEY' : '') +
    // (p.primaryKey === true && p.autoIncrement === true ? ' AUTOINCREMENT' : '') +
    (p.allowNull === false || p['null'] === false ? ' NOT NULL' : ' NULL') +
    (p.unique === true ? ' UNIQUE' : '') +
    (typeof p.default === "number" ? ' DEFAULT ' + p.default :'') +
    (typeof p.default === "string" ? ' DEFAULT \'' + p.default + '\'' :'');
};

Example usage:

var Data = schema.define('Data', {
        // num: { type: Number, primaryKey: true, autoIncrement: true},
    key: { type: String, index: true, unique: true, null: false },
    value: String
});

This will generate:

CREATE TABLE `Data` (
  `id` INTEGER PRIMARY KEY,
  `key` VARCHAR(255) NOT NULL UNIQUE,
  `value` VARCHAR(255) NULL
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions