Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLError 19 UNIQUE constraint failed

I am getting this error when setting up my app, creating a local database and simply inserting the first and only user (who has logged in locally). Please see comment in code for where I get the error message.

angular.module("greenApp")
.service("dbService",['$q', function($q){

    var db;
    var promise = function(){
        var deferred = $q.defer();

        db = window.openDatabase('greenDB', '1.0', 'Green Database', 2*1024*1024);

        db.transaction(function(tx){
            tx.executeSql("CREATE TABLE IF NOT EXISTS user (user TEXT PRIMARY KEY) ")
        }, function(err){
            alert('Something went wrong... Error: DATABASE INIT ' + err);
        }, function(scc){
            deferred.resolve();
        })

        return deferred.promise;
    }
    promise();

    var query = function(sql, args) {
        var deferred = $q.defer();

        db.transaction(function(tx) {
            tx.executeSql(sql, args, function(tx, results) {
                deferred.resolve(results);
            });
        }, function(err) {
            deferred.reject(err);
        });

        return deferred.promise;
    };

    var insert_into = function(args) {
        var queryPromise = query("INSERT INTO user (user) VALUES (?)", args);
        console.log("in insert_into", queryPromise) // Error message comes here
        return queryPromise;
    };

    return {
        promise: promise,
        insert_into: insert_into,
    };
}]);

Where args is simply ["user-name-string"], I get an error message that says:

"could not execute statement due to a constaint failure (19 UNIQUE constraint failed: user.user)

Any ideas what could have happened? Exactly the same code was running and working in a recent pure cordova project which I just ported to Ionic.

like image 281
novalain Avatar asked Jul 09 '15 21:07

novalain


People also ask

How do you resolve unique constraint failure?

If you want SQL to IGNORE that error and continue adding other records , then do this : INSERT or IGNORE into tablename VALUES (value1,value2 , so on ); If you want to replace the values in the table whenever the entry already exists , then do this: INSERT or REPLACE into tablename VALUES (value1,value2 , so on );

What does it mean by unique constraint failed?

The ORA-00001 message is triggered when a unique constraint has been violated. Essentially the user causes the error when trying to execute an INSERT or UPDATE statement that has generated a duplicate value in a restricted field.


1 Answers

It looks like you are inserting twice in your code ... here

var insert_into = function(args) {

    var queryPromise = query("INSERT INTO user (user) VALUES (?)", args);
    console.log("in insert_into", queryPromise) // Error message comes here
    return query("INSERT INTO user (user) VALUES (?)", args); <-- you did a query above, now you do it again?!?
};
like image 77
citywall Avatar answered Sep 29 '22 20:09

citywall