我正在使用 OpenAPI 3.0 设计和实现一个简单的 API,以允许对数据库实体进行基本的 CRUD 操作。
让我们假设一个实体Pet具有一些客户端给定的属性 ( name& age) 和一些生成的属性 ( id& created):
components:
schemas:
Pet:
type: object
properties:
id:
type: string
created:
type: string
format: date-time
name:
type: string
age:
type: integer
Run Code Online (Sandbox Code Playgroud)
我想指定 REST 端点来 POST、GET、PUT(如果可能的话 PATCH)宠物:
paths:
/pets:
post:
operationId: createPet
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
responses:
"200":
content:
application/json:
schema:
type: boolean
/pets/{id}:
parameters:
- name: id
schema:
type: string
in: path
required: true
get:
operationId: getPet
responses: …Run Code Online (Sandbox Code Playgroud) 我正在使用 JPA 为具有复合键的数据库定义数据模型。我无法更改数据库。
我有一个Car带有复合键(carType 和 carId)的实体。第二个(可选)PorscheInfo实体包含保时捷汽车的附加信息。不幸的是,相应的“porsche_info”表不包含包含 carType 信息的列,因为其条目专门引用CarType = 'Porsche'。
此操作的 SQL 很简单:
SELECT *
FROM cars
LEFT JOIN porsche_info
ON cars.CarId = porsche_info.CarId
AND cars.CarType = 'Porsche'
Run Code Online (Sandbox Code Playgroud)
如何将其转换为正确的 JPA 设置?
到目前为止,我有以下实体类:
@Embeddable
public class CarKey {
private String carType;
private String carId;
}
@Entity(name = "cars")
public class Car {
@EmbeddedId
private CarKey key;
// car information
@OneToOne
@JoinColumn(name = "CarId", referencedColumnName = "CarId")
private PorscheInfo porscheInfo;
// or
@OneToMany
@JoinColumn(name = "CarId", …Run Code Online (Sandbox Code Playgroud) 我想用psycopg2的sql子模块来写干净的动态SQL:
from psycopg2 import sql
...
cursor.execute(sql.SQL("SELECT * FROM {}").format(sql.Identifier('myschema.mytable'))
Run Code Online (Sandbox Code Playgroud)
这将创建以下查询:
SELECT * FROM "myschema.mytable"
Run Code Online (Sandbox Code Playgroud)
在这里我得到一个Relation "myschema.mytable" not found.例外。
如何正确处理架构名称?以下语句可以工作,但如何使用 psycopg2 创建它们?
SELECT * FROM myschema.mytable
SELECT * FROM myschema."mytable"
SELECT * FROM "myschema"."mytable"
Run Code Online (Sandbox Code Playgroud)
编辑:澄清架构前缀
dto ×1
hibernate ×1
jpa ×1
nullable ×1
one-to-many ×1
one-to-one ×1
openapi ×1
postgresql ×1
psycopg2 ×1
python ×1
rest ×1
swagger ×1