Jump to content

ON UPDATE/DELETE CASCADE (SQLite)


'LinKin

Recommended Posts

Hello,

I have this code which is NOT doing what I want..

When I change a team's name (via the script, not modifying it directly into the database with SQLite Browser), the registries at 'account_team' table linked to this team DON'T update their foreign key!

--------------------------------------------------------------------------------------------------------------------------------- 
--------------------                              Handled at < onResourceStart >                             -------------------- 
function initSQL() 
    ------------------------------------------------ 
    dbHandler = dbConnect("sqlite", "database.db") 
    ------------------------------------------------ 
     
     
        -- Creating 'teams' entity... 
    dbExec(dbHandler, "CREATE TABLE IF NOT EXISTS `teams` (`name` TEXT PRIMARY KEY, `tag` TEXT UNIQUE, `color` TEXT UNIQUE, `points` INTEGER, `description` TEXT)") 
     
     
        -- Creating 'accounts' entity... 
    dbExec(dbHandler, "CREATE TABLE IF NOT EXISTS `accounts` (`name` TEXT PRIMARY KEY, `last_nick` TEXT)") 
  
     
        -- Adding the missing column (because it references from 'teams' entity to 'accounts' entity ('accounts' is created AFTER 'teams)) 
    dbExec(dbHandler, "ALTER TABLE `teams` ADD COLUMN `owner` TEXT REFERENCES accounts(name) ON UPDATE CASCADE ON DELETE CASCADE") 
    dbExec(dbHandler, "CREATE UNIQUE INDEX IF NOT EXISTS unique_index ON teams(owner)") 
     
     
     
     
     
        -- Creating 'leader_team' entity... 
    dbExec(dbHandler, "CREATE TABLE IF NOT EXISTS `leader_team` (`account` TEXT PRIMARY KEY, `team` TEXT, FOREIGN KEY (account) REFERENCES accounts(name) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY (team) REFERENCES teams(name) ON UPDATE CASCADE ON DELETE CASCADE)") 
     
     
        -- Creating 'account_team' entity... 
    dbExec(dbHandler, "CREATE TABLE IF NOT EXISTS `account_team` (`account` TEXT, `team` TEXT, PRIMARY KEY (account, team), FOREIGN KEY (account) REFERENCES accounts(name) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY (team) REFERENCES teams(name) ON UPDATE CASCADE ON DELETE CASCADE)") 
     
     
        -- Creating 'join_requests' entity... 
    dbExec(dbHandler, "CREATE TABLE IF NOT EXISTS `join_requests` (`account` TEXT PRIMARY KEY, `team` TEXT, FOREIGN KEY (account) REFERENCES accounts(name) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY (team) REFERENCES teams(name) ON UPDATE CASCADE ON DELETE CASCADE)") 
end 
--------------------------------------------------------------------------------------------------------------------------------- 

Example:

Data before the UPDATE:

TEAMS TABLE:
nametagcolorpointsdescriptionowner
Street Warriors|SW|#00FFFF0BlablahLinKin

ACCOUNT_TEAM TABLE:
accountteam
LinKinStreet Warriors

Data after the UPDATE:

TEAMS TABLE:
nametagcolorpointsdescriptionowner
Street Warr|SW|#00FFFF0BlablahLinKin

ACCOUNT_TEAM TABLE:
accountteam
LinKinStreet Warriors

As you see, the table ACCOUNT_TEAM didn't update its foreing key for the 'team' column.. Breaking the database integrity.

Link to comment

And why aren't you using ID's which would be auto increment? Those shouldn't change whenever you edit your data making these things really... impossible to occur. I've always had trouble with cascades and therefore wrote then scriptwise myself. Too bad that MTA takes a shitload of memory to do it scriptwise...

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...