1

Adding more database constraints

This commit is contained in:
Alex Yatskov 2015-04-18 14:23:35 +09:00
parent 5c9401f3cc
commit 4896f52871
4 changed files with 22 additions and 16 deletions

View File

@ -91,7 +91,7 @@ conn.query('CREATE TABLE history(date DATETIME NOT NULL, reviewId INT NOT NULL,
// //
conn.query('DROP TABLE IF EXISTS historyGroups'); conn.query('DROP TABLE IF EXISTS historyGroups');
conn.query('CREATE TABLE historyGroups(categoryId INT NOT NULL, categoryValue FLOAT NOT NULL, historyId INT NOT NULL, FOREIGN KEY(historyId) REFERENCES history(id))'); conn.query('CREATE TABLE historyGroups(categoryId INT NOT NULL, categoryValue FLOAT NOT NULL, historyId INT NOT NULL, FOREIGN KEY(historyId) REFERENCES history(id), FOREIGN KEY(categoryId) REFERENCES categories(id))');
// //

View File

@ -77,7 +77,9 @@ CREATE TABLE `historyGroups` (
`categoryValue` float NOT NULL, `categoryValue` float NOT NULL,
`historyId` int(11) NOT NULL, `historyId` int(11) NOT NULL,
KEY `historyId` (`historyId`), KEY `historyId` (`historyId`),
CONSTRAINT `historyGroups_ibfk_1` FOREIGN KEY (`historyId`) REFERENCES `history` (`id`) KEY `categoryId` (`categoryId`),
CONSTRAINT `historyGroups_ibfk_1` FOREIGN KEY (`historyId`) REFERENCES `history` (`id`),
CONSTRAINT `historyGroups_ibfk_2` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
@ -133,4 +135,4 @@ UNLOCK TABLES;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2015-03-26 13:31:52 -- Dump completed on 2015-04-18 13:51:01

View File

@ -185,17 +185,9 @@ func removeCategory(rw http.ResponseWriter, req *http.Request) {
return return
} }
result, err := db.Exec("DELETE FROM categories WHERE id = (?)", request.Id) _, err := db.Exec("DELETE FROM categories WHERE id = (?)", request.Id)
if err != nil {
log.Fatal(err)
}
affectedRows, err := result.RowsAffected() js, err := json.Marshal(jsonRemoveCategoryResponse{err == nil})
if err != nil {
log.Fatal(err)
}
js, err := json.Marshal(jsonRemoveCategoryResponse{affectedRows > 0})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -236,6 +228,17 @@ func accessReview(rw http.ResponseWriter, req *http.Request) {
} }
for id, value := range request.Profile { for id, value := range request.Profile {
catRow := db.QueryRow("SELECT EXISTS(SELECT NULL FROM categories WHERE id = ?)", id)
var catExists int
if err := catRow.Scan(&catExists); err != nil {
log.Fatal(err)
}
if catExists == 0 {
continue
}
if _, err := db.Exec("INSERT INTO historyGroups(categoryId, categoryValue, historyId) VALUES(?, ?, ?)", id, value, insertId); err != nil { if _, err := db.Exec("INSERT INTO historyGroups(categoryId, categoryValue, historyId) VALUES(?, ?, ?)", id, value, insertId); err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -57,6 +57,9 @@
$(this).remove(); $(this).remove();
}); });
} }
else {
alert('Category could not be deleted because it is referenced by user access history.');
}
}, 'json'); }, 'json');
} }
@ -69,9 +72,7 @@
}); });
$('#categories button').unbind().click(function() { $('#categories button').unbind().click(function() {
if (confirm('Are you sure you want to delete this category?')) { removeCategory(parseInt($(this).attr('data-categoryId')));
removeCategory(parseInt($(this).attr('data-categoryId')));
}
}); });
} }