I have the the following Django model:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
full_name = models.CharField(max_length=100)
network = models.ForeignKey(Network)
positions = models.ManyToManyField(Position, through ='Timestamp', blank=True)
featured_video = models.ForeignKey(VideoInfo, blank=True, null=True)
From SHOW CREATE TABLE userprofile_userprofile
, I get the following MySQL output:
| userprofile_userprofile | CREATE TABLE `userprofile_userprofile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`full_name` varchar(100) NOT NULL,
`network_id` int(11) NOT NULL,
`featured_video_id` int(11) DEFAULT NULL,
`bio` longtext NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`),
KEY `userprofile_userprofile_fb2a452b` (`network_id`),
KEY `userprofile_userprofile_58bbe944` (`featured_video_id`),
CONSTRAINT `featured_video_id_refs_id_b7c2ab56` FOREIGN KEY (`featured_video_id`) REFERENCES `userprofile_videoinfo` (`id`),
CONSTRAINT `network_id_refs_id_f5c27879` FOREIGN KEY (`network_id`) REFERENCES `network_network` (`id`),
CONSTRAINT `user_id_refs_id_da7416c6` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=31034 DEFAULT CHARSET=latin1 |
What do the last seven lines doing here? What significance do they have?
unique key
key
constraint
engine=InnoDB
auto-increment=31034
- key is creating an index
- unique key is creating an index that enforces a unique value for the column mentioned.
- constraint ... Foreign Key is creating a foreign key reference, limiting values inserted to only those that already exist in the referenced table.
- auto_increment set the starting auto_increment column value.
精彩评论