orm

ORM多表增删改查

故事扮演 提交于 2020-02-24 07:46:43
一 创建多表 在 models.py 里创建 4 张表: Author (作者)、 AuthorDetail (作者详细信息)、 Publish (出版社)、 Book (书) 四张表关系为: ( 1 )首先创建一对一关系。 OneToOneField() 创建 Author 表 class Author(models.Model): name=models.CharField( max_length=32) age=models.IntegerField() authorDetail=models.OneToOneField(to="AuthorDetail",to_field="id",on_delete=models.CASCADE) models.OneToOneField 是创建一对一关系的关键字, to="AuthorDetail" 表示和 AuthorDetail 表创建一对一关系。 to_field="id" 表示关联字段,不写默认为 Id (主键)。 on_delete=models.CASCADE 表示级联删除。(注: to 后面的表名和字段都是字符串形式)。创建一对一关系,生成 authorDetail ,这个为关联属性。通过 Author 找 AuthorDetail 表中的内容就要通过这个关联属性。(生成一对一的表,会在关联的表生成字段:

ORM基本操作回顾

二次信任 提交于 2020-02-23 13:41:40
连接数据库 默认是 MySQLdb 指定引擎 dialect[+driver]: //user:password@host/dbname[?key=value..]: from sqlalchemy import create_engine engine = create_engine("mysql+pymysql://root:root@127.0.0.1/ormreview?charset=utf8", encoding='utf8') # 判断是否连接成功 with engine.connect() as f: result = f.execute("select 1") print(result.fetchone()) print(type(result)) >>> # (1,) #<class 'sqlalchemy.engine.result.ResultProxy'> ORM模型创建 必须继承自sqlalchemy提供的基类 from sqlalchemy.ext.declarative import declarative_base from datetime import datetime engine = create_engine("mysql+pymysql://root:root@127.0.0.1/ormreview?charset=utf8",

国内开源ORM组件 ELinq正式版发布

大兔子大兔子 提交于 2020-02-23 11:11:36
前言:    自从发布 年度开源力作-国产ORM框架ELinq诞生了 这篇博文后,得到了很多园友的支持,另外 ELinq 群:271342583群的群友也非常活跃,大家一起为该框架起了一个响亮的名字: ELinq (之前为 NLite.Data ),另外一些网友建议的新特性,比如支持类似EF框架的DbContext,可以直接继承DbContext,而不需要手工实现Dispose,可以直接定义DbSet类型的成员属性,而不需要手工对其赋值,增加在调试时可以直接方便的看到Sql内容,而不是通过日志,可以方便的查看SQL的参数等等,为了快速的回馈广大网页的支持特发布该版本表示感谢! 更新日志:     ELinq 0.4 2012/12/8 发布: 1. 组件名称由NLite.Data 改为ELinq,命名空间没变,保持良好的向下兼容性 2. 开放DbContext 类,使其允许被直接继承,简化ELinq使用DbContext的复杂度 3. 开放DbSet 的SqlText和ExecutePlan 属性接口,方便DbSet在运行时方便的调试,可以直接查看SQL和执行计划 NLite.Data 2012-12-3 RC2 发布 1. 修复通过HashTable进行删除的Bug 2. 支持通过connectionString 和providerName 作为参数来配置 3. 使MySQL

Sprng Data JPA与hibernate的关系

夙愿已清 提交于 2020-02-23 09:59:47
1.ORM 概述 ORM ( Object-Relational Mapping ) 表示对象关系映射。在面向对象的软件开发中,通过 ORM ,就可以把对象映射到关系型数据库中。只要有一套程序能够做到建立对象与数据库的关联,操作对象就可以直接操作数据库数据,就可以说这套程序实现了 ORM 对象关系映射 简单的说: ORM 就是建立实体类和数据库表之间的关系,从而达到操作实体类就相当于操作数据库表的目的。 1.1 为什么使用 ORM 当实现一个应用程序时(不使用 O/R Mapping ),我们可能会写特别多数据访问层的代码,从数据库保存数据、修改数据、删除数据,而这些代码都是重复的。而使用 ORM 则会大大减少重复性代码。对象关系映射( Object Relational Mapping ,简称 ORM ),主要实现程序对象到关系数据库数据的映射。 1.2 常见 ORM 框架 常见的 orm 框架: Mybatis ( ibatis )、 Hibernate 、 Jpa 2.hibernate 与 JPA 的概述 [ 了解 ] 2.1 hibernate 概述 Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,它将 POJO 与数据库表建立映射关系,是一个全自动的 orm 框架, hibernate 可以自动生成 SQL 语句

How to create an Hstore column type in Laravel 4?

谁说我不能喝 提交于 2020-02-22 08:21:38
问题 I'm using the Schema Builder inside of Migrations in Laravel 4 to manage my Postgres database during development. I've run into a problem. I want to utilize the Postgres Hstore column type in my table but I can't figure it out. There doesn't seem to be a "custom" column type in the Schema Builder (that I can find), so this was my attempt inside of a migration. This didn't work for me. Schema::create('entities', function(Blueprint $table) { $table->bigIncrements('id'); $table->string('type');

Django(三) ORM操作

人盡茶涼 提交于 2020-02-22 04:35:50
一、DjangoORM 创建基本类型及生成数据库表结构 1、简介   ORM:关系对象映射。定义一个类自动生成数据库的表结构。   数据库常用的数据类型 : 数字 字符串 时间 ORM分为两种类型: 主流都是code first code first:先写代码,执行代码创建数据库表结构 DB first:据库里先创建数据库表结构,根据表结构生成类,根据类操作数据库   Django也是code first。其本质: 根据类自动创建数据库表       2.根据类对数据库中的表进行各种操作 2、创建数据库 表结构   2.1 先写类 1 from django.db import models 2 3 class UserInfo(models.Model): # 必须继承models.Model 4 # 不写则,django默认创建ID列,自增,主键 5 # 用户名列,字符串类型,指定长度 6 username = models.CharField(max_length=32) 7 password = models.CharField(max_length=64)   2.2 注册app         执行命令 python manage.py makemigrations ,会提示 No changes detected ,    这是因为:执行命令时会找所有models

Where to find the DTD of Hibernate?

痞子三分冷 提交于 2020-02-21 13:20:48
问题 The DTD in the hibernate jar is a good way to know what are the attributes that can be included and the expected name for that tag. Opening up the DTD file is the easiest way to get an overview of all elements and attributes, and to view the defaults, as well as some comments. This will help the programmer to write hibernate.cfg.xml file from scracth(crazy but some people do ask this to achieve without using internet :O ) Please tell where to locate the DTD when we have a hibernate jar. This

Where to find the DTD of Hibernate?

大兔子大兔子 提交于 2020-02-21 13:16:25
问题 The DTD in the hibernate jar is a good way to know what are the attributes that can be included and the expected name for that tag. Opening up the DTD file is the easiest way to get an overview of all elements and attributes, and to view the defaults, as well as some comments. This will help the programmer to write hibernate.cfg.xml file from scracth(crazy but some people do ask this to achieve without using internet :O ) Please tell where to locate the DTD when we have a hibernate jar. This

Entity Framework 6 - inheritance and navigation properties on base class

喜欢而已 提交于 2020-02-21 06:30:13
问题 I have a problem with navigation properties and inheritance. This is my problem: I have a base Person class and classes User and Worker which inherit from Person . On the DB level I'm using single table inheritance or table per hierarchy (TPH) inheritance. So there a single table with a discriminator column. Both User and Worker need to have a Company relation, so I would like to define it on the Person class. I define my model like this: [Table("mydb.person")] public abstract partial class

Spring Data JPA的概述

时间秒杀一切 提交于 2020-02-20 13:36:47
Spring Data JPA的概述 1.1 Spring Data JPA概述 Spring Data JPA 是 Spring 基于 ORM 框架、JPA规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和操作。它提供了包括增删改查等在内的常用功能,且易于扩展!学习并使用Spring Data JPA 可以极大提高开发效率! Spring Data JPA 让我们解脱了DAO层的操作,基本上所有CRUD都可以依赖于它来实现,在实际的工作工程中,推荐使用Spring Data JPA +ORM(如:hibernate)完成操作,这样在切换不同的ORM框架时提供了极大的方便,同时也使数据库层操作更加简单,方便解耦 1.2 Spring Data JPA的特性 SpringData Jpa 极大简化了数据库访问层代码。 如何简化的呢? 使用了SpringDataJpa,我们的dao层中只需要写接口,就自动具有了增删改查、分页查询等方法。 1.3 Spring Data JPA 与 JPA和hibernate之间的关系 JPA是一套规范,内部是有接口和抽象类组成的。hibernate是一套成熟的ORM框架,而且Hibernate实现了JPA规范,所以也可以称hibernate为JPA的一种实现方式,我们使用JPA的API编程