dango models and database ---- relation ship

 

一、django自带的ORM中可以定义表与表之间的对应关系、现比较一下各个不同关系之间数据库端的实现

  1、ForeignKey(ManyToOne)关系

from django.db import models

# Create your models here.

class User(models.Model):
    name=models.CharField(max_length=30)
    phoneNumber=models.BigIntegerField()

class Blog(models.Model):
    user=models.ForeignKey(User)
    blogName=models.CharField(max_length=16)
    content=models.TextField()

# 
CREATE TABLE `sitea_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `phoneNumber` bigint(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



CREATE TABLE `sitea_blog` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `blogName` varchar(16) NOT NULL,
  `content` longtext NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `sitea_blog_user_id_2c1e69b4_fk_sitea_user_id` (`user_id`),
  CONSTRAINT `sitea_blog_user_id_2c1e69b4_fk_sitea_user_id` FOREIGN KEY (`user_id`) REFERENCES `sitea_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

   2、ManyToManyField关系

from django.db import models

# Create your models here.

class User(models.Model):
    name=models.CharField(max_length=30)
    phoneNumber=models.BigIntegerField()

class Blog(models.Model):
    user=models.ManyToManyField(User)
    blogName=models.CharField(max_length=16)
    content=models.TextField()
CREATE TABLE `sitea_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `phoneNumber` bigint(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

CREATE TABLE `sitea_blog` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `blogName` varchar(16) NOT NULL,
  `content` longtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `sitea_blog_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `blog_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `sitea_blog_user_blog_id_user_id_cdb4869e_uniq` (`blog_id`,`user_id`),
  KEY `sitea_blog_user_user_id_a960fdcf_fk_sitea_user_id` (`user_id`),
  CONSTRAINT `sitea_blog_user_blog_id_72f927d9_fk_sitea_blog_id` FOREIGN KEY (`blog_id`) REFERENCES `sitea_blog` (`id`),
  CONSTRAINT `sitea_blog_user_user_id_a960fdcf_fk_sitea_user_id` FOREIGN KEY (`user_id`) REFERENCES `sitea_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 由上面可以看出ManyToMany 关系也是和通常的作法差不多、 加一个关联表

  3、OneToOne关系

from django.db import models

# Create your models here.

class User(models.Model):
    name=models.CharField(max_length=30)
    phoneNumber=models.BigIntegerField()

class Blog(models.Model):
    user=models.OneToOneField(User,default=1)
    blogName=models.CharField(max_length=16)
    content=models.TextField()
CREATE TABLE `sitea_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `phoneNumber` bigint(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `sitea_blog` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `blogName` varchar(16) NOT NULL,
  `content` longtext NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`),
  CONSTRAINT `sitea_blog_user_id_2c1e69b4_fk_sitea_user_id` FOREIGN KEY (`user_id`) REFERENCES `sitea_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

 

 

 

---

posted on 2017-05-26 15:00  蒋乐兴的技术随笔  阅读(444)  评论(0编辑  收藏  举报

导航