zoukankan      html  css  js  c++  java
  • 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 ;

    ---

  • 相关阅读:
    面试题汇总
    桥接模式
    2010412 面试题
    2010412 面试题1
    访问者模式
    原码、反码、补码什么意思?有什么用?
    装饰模式
    mysql 忘记root密码 进行重置
    运维开源工具一览
    编写一个函数计算小费,小费为总账单的20%
  • 原文地址:https://www.cnblogs.com/JiangLe/p/6908532.html
Copyright © 2011-2022 走看看