Index: /trunk/contrib/znc-import.go
===================================================================
--- /trunk/contrib/znc-import.go	(revision 420)
+++ /trunk/contrib/znc-import.go	(revision 421)
@@ -115,6 +115,7 @@
 			log.Fatalf("failed to store user %q: %v", username, err)
 		}
-
-		l, err := db.ListNetworks(username)
+		userID := u.ID
+
+		l, err := db.ListNetworks(userID)
 		if err != nil {
 			log.Fatalf("failed to list networks for user %q: %v", username, err)
@@ -182,5 +183,5 @@
 			n.Pass = pass
 
-			if err := db.StoreNetwork(username, n); err != nil {
+			if err := db.StoreNetwork(userID, n); err != nil {
 				logger.Fatalf("failed to store network: %v", err)
 			}
Index: /trunk/db.go
===================================================================
--- /trunk/db.go	(revision 420)
+++ /trunk/db.go	(revision 421)
@@ -71,5 +71,5 @@
 	id INTEGER PRIMARY KEY,
 	name VARCHAR(255),
-	user VARCHAR(255) NOT NULL,
+	user INTEGER NOT NULL,
 	addr VARCHAR(255) NOT NULL,
 	nick VARCHAR(255) NOT NULL,
@@ -83,6 +83,7 @@
 	sasl_external_cert BLOB DEFAULT NULL,
 	sasl_external_key BLOB DEFAULT NULL,
-	FOREIGN KEY(user) REFERENCES User(username),
-	UNIQUE(user, addr, nick)
+	FOREIGN KEY(user) REFERENCES User(id),
+	UNIQUE(user, addr, nick),
+	UNIQUE(user, name)
 );
 
@@ -116,4 +117,34 @@
 		ALTER TABLE UserNew RENAME TO User;
 	`,
+	`
+		CREATE TABLE NetworkNew (
+			id INTEGER PRIMARY KEY,
+			name VARCHAR(255),
+			user INTEGER NOT NULL,
+			addr VARCHAR(255) NOT NULL,
+			nick VARCHAR(255) NOT NULL,
+			username VARCHAR(255),
+			realname VARCHAR(255),
+			pass VARCHAR(255),
+			connect_commands VARCHAR(1023),
+			sasl_mechanism VARCHAR(255),
+			sasl_plain_username VARCHAR(255),
+			sasl_plain_password VARCHAR(255),
+			sasl_external_cert BLOB DEFAULT NULL,
+			sasl_external_key BLOB DEFAULT NULL,
+			FOREIGN KEY(user) REFERENCES User(id),
+			UNIQUE(user, addr, nick),
+			UNIQUE(user, name)
+		);
+		INSERT INTO NetworkNew
+			SELECT Network.id, name, User.id as user, addr, nick,
+				Network.username, realname, pass, connect_commands,
+				sasl_mechanism, sasl_plain_username, sasl_plain_password,
+				sasl_external_cert, sasl_external_key
+			FROM Network
+			JOIN User ON Network.user = User.username;
+		DROP TABLE Network;
+		ALTER TABLE NetworkNew RENAME TO Network;
+	`,
 }
 
@@ -264,5 +295,5 @@
 }
 
-func (db *DB) DeleteUser(username string) error {
+func (db *DB) DeleteUser(id int64) error {
 	db.lock.Lock()
 	defer db.lock.Unlock()
@@ -280,15 +311,15 @@
 			JOIN Network ON Channel.network = Network.id
 			WHERE Network.user = ?
-		)`, username)
-	if err != nil {
-		return err
-	}
-
-	_, err = tx.Exec("DELETE FROM Network WHERE user = ?", username)
-	if err != nil {
-		return err
-	}
-
-	_, err = tx.Exec("DELETE FROM User WHERE username = ?", username)
+		)`, id)
+	if err != nil {
+		return err
+	}
+
+	_, err = tx.Exec("DELETE FROM Network WHERE user = ?", id)
+	if err != nil {
+		return err
+	}
+
+	_, err = tx.Exec("DELETE FROM User WHERE id = ?", id)
 	if err != nil {
 		return err
@@ -298,5 +329,5 @@
 }
 
-func (db *DB) ListNetworks(username string) ([]Network, error) {
+func (db *DB) ListNetworks(userID int64) ([]Network, error) {
 	db.lock.RLock()
 	defer db.lock.RUnlock()
@@ -307,5 +338,5 @@
 		FROM Network
 		WHERE user = ?`,
-		username)
+		userID)
 	if err != nil {
 		return nil, err
@@ -343,5 +374,5 @@
 }
 
-func (db *DB) StoreNetwork(username string, network *Network) error {
+func (db *DB) StoreNetwork(userID int64, network *Network) error {
 	db.lock.Lock()
 	defer db.lock.Unlock()
@@ -386,5 +417,5 @@
 				sasl_plain_password, sasl_external_cert, sasl_external_key)
 			VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
-			username, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands,
+			userID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands,
 			saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob,
 			network.SASL.External.PrivKeyBlob)
Index: /trunk/downstream.go
===================================================================
--- /trunk/downstream.go	(revision 420)
+++ /trunk/downstream.go	(revision 421)
@@ -1017,5 +1017,5 @@
 			}
 			n.Nick = nick
-			err = dc.srv.db.StoreNetwork(dc.user.Username, &n.Network)
+			err = dc.srv.db.StoreNetwork(dc.user.ID, &n.Network)
 		})
 		if err != nil {
@@ -1698,5 +1698,5 @@
 	n.SASL.Plain.Username = username
 	n.SASL.Plain.Password = password
-	if err := dc.srv.db.StoreNetwork(dc.user.Username, &n.Network); err != nil {
+	if err := dc.srv.db.StoreNetwork(dc.user.ID, &n.Network); err != nil {
 		dc.logger.Printf("failed to save NickServ credentials: %v", err)
 	}
Index: /trunk/service.go
===================================================================
--- /trunk/service.go	(revision 420)
+++ /trunk/service.go	(revision 421)
@@ -549,5 +549,5 @@
 	net.SASL.Mechanism = "EXTERNAL"
 
-	if err := dc.srv.db.StoreNetwork(net.Username, &net.Network); err != nil {
+	if err := dc.srv.db.StoreNetwork(dc.user.ID, &net.Network); err != nil {
 		return err
 	}
@@ -594,5 +594,5 @@
 	net.SASL.Mechanism = "PLAIN"
 
-	if err := dc.srv.db.StoreNetwork(net.Username, &net.Network); err != nil {
+	if err := dc.srv.db.StoreNetwork(dc.user.ID, &net.Network); err != nil {
 		return err
 	}
@@ -618,5 +618,5 @@
 	net.SASL.Mechanism = ""
 
-	if err := dc.srv.db.StoreNetwork(dc.user.Username, &net.Network); err != nil {
+	if err := dc.srv.db.StoreNetwork(dc.user.ID, &net.Network); err != nil {
 		return err
 	}
@@ -690,5 +690,5 @@
 	u.stop()
 
-	if err := dc.srv.db.DeleteUser(username); err != nil {
+	if err := dc.srv.db.DeleteUser(dc.user.ID); err != nil {
 		return fmt.Errorf("failed to delete user: %v", err)
 	}
Index: /trunk/user.go
===================================================================
--- /trunk/user.go	(revision 420)
+++ /trunk/user.go	(revision 421)
@@ -315,5 +315,5 @@
 	defer close(u.done)
 
-	networks, err := u.srv.db.ListNetworks(u.Username)
+	networks, err := u.srv.db.ListNetworks(u.ID)
 	if err != nil {
 		u.srv.Logger.Printf("failed to list networks for user %q: %v", u.Username, err)
@@ -509,5 +509,5 @@
 
 	network := newNetwork(u, record, nil)
-	err := u.srv.db.StoreNetwork(u.Username, &network.Network)
+	err := u.srv.db.StoreNetwork(u.ID, &network.Network)
 	if err != nil {
 		return nil, err
@@ -529,5 +529,5 @@
 	}
 
-	if err := u.srv.db.StoreNetwork(u.Username, record); err != nil {
+	if err := u.srv.db.StoreNetwork(u.ID, record); err != nil {
 		return nil, err
 	}
