Index: trunk/db.go
===================================================================
--- trunk/db.go	(revision 663)
+++ trunk/db.go	(revision 664)
@@ -113,4 +113,11 @@
 }
 
+func GetNick(user *User, net *Network) string {
+	if net.Nick != "" {
+		return net.Nick
+	}
+	return user.Username
+}
+
 func GetRealname(user *User, net *Network) string {
 	if net.Realname != "" {
@@ -120,5 +127,5 @@
 		return user.Realname
 	}
-	return net.Nick
+	return GetNick(user, net)
 }
 
Index: trunk/db_postgres.go
===================================================================
--- trunk/db_postgres.go	(revision 663)
+++ trunk/db_postgres.go	(revision 664)
@@ -37,5 +37,5 @@
 	"user" INTEGER NOT NULL REFERENCES "User"(id) ON DELETE CASCADE,
 	addr VARCHAR(255) NOT NULL,
-	nick VARCHAR(255) NOT NULL,
+	nick VARCHAR(255),
 	username VARCHAR(255),
 	realname VARCHAR(255),
@@ -78,4 +78,5 @@
 var postgresMigrations = []string{
 	"", // migration #0 is reserved for schema initialization
+	`ALTER TABLE "Network" ALTER COLUMN nick DROP NOT NULL`,
 }
 
@@ -260,7 +261,7 @@
 	for rows.Next() {
 		var net Network
-		var name, username, realname, pass, connectCommands sql.NullString
+		var name, nick, username, realname, pass, connectCommands sql.NullString
 		var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
-		err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname,
+		err := rows.Scan(&net.ID, &name, &net.Addr, &nick, &username, &realname,
 			&pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword,
 			&net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob, &net.Enabled)
@@ -269,4 +270,5 @@
 		}
 		net.Name = name.String
+		net.Nick = nick.String
 		net.Username = username.String
 		net.Realname = realname.String
@@ -292,4 +294,5 @@
 
 	netName := toNullString(network.Name)
+	nick := toNullString(network.Nick)
 	netUsername := toNullString(network.Username)
 	realname := toNullString(network.Realname)
@@ -321,5 +324,5 @@
 			VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
 			RETURNING id`,
-			userID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands,
+			userID, netName, network.Addr, nick, netUsername, realname, pass, connectCommands,
 			saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob,
 			network.SASL.External.PrivKeyBlob, network.Enabled).Scan(&network.ID)
@@ -332,5 +335,5 @@
 				enabled = $14
 			WHERE id = $1`,
-			network.ID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands,
+			network.ID, netName, network.Addr, nick, netUsername, realname, pass, connectCommands,
 			saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob,
 			network.SASL.External.PrivKeyBlob, network.Enabled)
Index: trunk/db_sqlite.go
===================================================================
--- trunk/db_sqlite.go	(revision 663)
+++ trunk/db_sqlite.go	(revision 664)
@@ -29,5 +29,5 @@
 	user INTEGER NOT NULL,
 	addr TEXT NOT NULL,
-	nick TEXT NOT NULL,
+	nick TEXT,
 	username TEXT,
 	realname TEXT,
@@ -139,4 +139,34 @@
 	"ALTER TABLE Network ADD COLUMN enabled INTEGER NOT NULL DEFAULT 1",
 	"ALTER TABLE User ADD COLUMN realname VARCHAR(255)",
+	`
+		CREATE TABLE NetworkNew (
+			id INTEGER PRIMARY KEY,
+			name TEXT,
+			user INTEGER NOT NULL,
+			addr TEXT NOT NULL,
+			nick TEXT,
+			username TEXT,
+			realname TEXT,
+			pass TEXT,
+			connect_commands TEXT,
+			sasl_mechanism TEXT,
+			sasl_plain_username TEXT,
+			sasl_plain_password TEXT,
+			sasl_external_cert BLOB,
+			sasl_external_key BLOB,
+			enabled INTEGER NOT NULL DEFAULT 1,
+			FOREIGN KEY(user) REFERENCES User(id),
+			UNIQUE(user, addr, nick),
+			UNIQUE(user, name)
+		);
+		INSERT INTO NetworkNew
+			SELECT id, name, user, addr, nick, username, realname, pass,
+				connect_commands, sasl_mechanism, sasl_plain_username,
+				sasl_plain_password, sasl_external_cert, sasl_external_key,
+				enabled
+			FROM Network;
+		DROP TABLE Network;
+		ALTER TABLE NetworkNew RENAME TO Network;
+	`,
 }
 
@@ -394,7 +424,7 @@
 	for rows.Next() {
 		var net Network
-		var name, username, realname, pass, connectCommands sql.NullString
+		var name, nick, username, realname, pass, connectCommands sql.NullString
 		var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
-		err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname,
+		err := rows.Scan(&net.ID, &name, &net.Addr, &nick, &username, &realname,
 			&pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword,
 			&net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob, &net.Enabled)
@@ -403,4 +433,5 @@
 		}
 		net.Name = name.String
+		net.Nick = nick.String
 		net.Username = username.String
 		net.Realname = realname.String
@@ -447,5 +478,5 @@
 		sql.Named("name", toNullString(network.Name)),
 		sql.Named("addr", network.Addr),
-		sql.Named("nick", network.Nick),
+		sql.Named("nick", toNullString(network.Nick)),
 		sql.Named("username", toNullString(network.Username)),
 		sql.Named("realname", toNullString(network.Realname)),
Index: trunk/downstream.go
===================================================================
--- trunk/downstream.go	(revision 663)
+++ trunk/downstream.go	(revision 664)
@@ -103,5 +103,5 @@
 		"name":     irc.TagValue(network.GetName()),
 		"state":    irc.TagValue(state),
-		"nickname": irc.TagValue(network.Nick),
+		"nickname": irc.TagValue(GetNick(&network.user.User, &network.Network)),
 	}
 
@@ -336,5 +336,5 @@
 	// configured nickname and hope it was the one being used when we were
 	// connected.
-	return net.casemap(nick) == net.casemap(net.Nick)
+	return net.casemap(nick) == net.casemap(GetNick(&net.user.User, &net.Network))
 }
 
@@ -2415,4 +2415,7 @@
 			}
 
+			if record.Nick == dc.user.Username {
+				record.Nick = ""
+			}
 			if record.Realname == dc.user.Realname {
 				record.Realname = ""
@@ -2456,4 +2459,7 @@
 			}
 
+			if record.Nick == dc.user.Username {
+				record.Nick = ""
+			}
 			if record.Realname == dc.user.Realname {
 				record.Realname = ""
Index: trunk/service.go
===================================================================
--- trunk/service.go	(revision 663)
+++ trunk/service.go	(revision 664)
@@ -485,5 +485,4 @@
 	record := &Network{
 		Addr:    *fs.Addr,
-		Nick:    dc.nick,
 		Enabled: true,
 	}
Index: trunk/upstream.go
===================================================================
--- trunk/upstream.go	(revision 663)
+++ trunk/upstream.go	(revision 664)
@@ -1672,5 +1672,5 @@
 
 func (uc *upstreamConn) register() {
-	uc.nick = uc.network.Nick
+	uc.nick = GetNick(&uc.user.User, &uc.network.Network)
 	uc.nickCM = uc.network.casemap(uc.nick)
 	uc.username = uc.network.GetUsername()
