首页 > 代码库 > dango models and database ---- relation ship

dango models and database ---- relation ship

 

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

  1、ForeignKey关系

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 ;

 

 

 

---

dango models and database ---- relation ship