当前位置:网站首页>Zero code implements one-to-one table relationship and cascading save of infinite primary and child tables

Zero code implements one-to-one table relationship and cascading save of infinite primary and child tables

2022-06-24 05:12:00 crudapi

Table relation higher order

In the last article Table relationship management in , Introduces one to many 、 For one more 、 And many to many relationships , This paper mainly introduces one-to-one relation and infinite master sub table in crudapi Applications in the system .

Summary

one-on-one

One to one relationship refers to a relationship between two tables in a relational database . In a relational database, a single row in the first table can only be related to one row in the second table , And a row in the second table can only be related to a row in the first table .

In a one to many relationship , The foreign key is set up in a subtable ; In a one-on-one relationship , Foreign keys can be created in the main table or sub table , For consistency ,crudapi In the system, the one-to-one relationship of foreign keys is also established in the sub table , The advantage is that if you need to remove the table relationship in the future , There is no need to modify the main table structure .

Customer information

Customer customer In the main table , It usually stores basic information , If you have more information, you can consider storing it separately in the customer information customerProfile In the table , They can be implemented in a one-to-one relationship .

Customers and customer profiles : one-on-one ( Main sub direction )

customer

Before creating a customer customer The table remains unchanged

customerProfile

Create customer profiles customerProfile surface , It mainly includes customer number field 、 Birthday 、 Gender 、 Hobbies, etc , Where customer number customerId Fields are used to establish table relationships

profileRelation

Establish a one-to-one relationship between master and slave directions , Customer customer The number of the table id Field , Point to customer information customerProfile Customer number of customerId Field , The English name of the relationship profile For querying associated objects , Set the navigation property name to profile.

Customer profile and customer : one-on-one ( The main direction is )

customerRelation

Establish a one-to-one relationship between sub principal directions , Customer information customerProfile Customer number of customerId Field , Point to the customer customer The number of the table id Field , The English name of the relationship customer For querying associated objects , Set the navigation property name to customer, It's a bit similar to the previous order and customer many to one relationship , The associated objects are customer object , But at that time, multiple orders could be associated with the same customer , Now a customer profile can only be associated with one customer .

Cascade saving of customer master sub tables

createCustomer

ui Create customer , At the same time, input sub table customer information ,chrome Open network request record , You can see POST body as follows :

{
    "name": " Liu bei ",
    "mobile": "13699998888",
    "email": "[email protected]",
    "profile": {
        "name": " Liu Bei's information ",
        "birthday": "2021-02-14",
        "sex": " male ",
        "hobby": " Riding a horse "
    }
}

among profile For customer information ,

editCustomer

Check customer details , Customer discovery and customer information are saved successfully at one time ! In the last article , Sales order and order line are one to many primary sub relationships ,

The subtable is in the form of an array , In a one-to-one master-child relationship, a subtable is in the form of an object , The representation is the same in the database , Foreign keys are all built in sub tables .

Direct operation of customer information

editCustomerProfile

You can also create a customer profile directly , Choose to hang under the designated customer , Can achieve the same effect .

Infinite sub table

By setting table relationships , One to many and one to one ( Main sub direction ) In theory, it can go on indefinitely , All tables are saved in cascade at one time , For example, provinces and cities can usually achieve 3 Stage sub table , The catalog file belongs to an infinite sub table .

Three level sub table of province and city

ssqGraph

There is a one to many relationship between province and city , City and district are one to many .

ssq

ui effect , Jiangsu Province includes Nanjing and Huai'an , Nanjing also includes Jiangning District and Yuhuatai District .

Directory infinite sub table

The first level directory can include subdirectories and files , If it's a subdirectory , Subdirectories can continue to include subdirectories and files

folder1

Graph catalog infinite sub table -1

folder2

Graph catalog infinite sub table -2

ui effect , Sub tables include directories and files , It can go on , Because of the screen size , I don't want to show them all here .

Query directory list

folders

Summary

This article introduces the one-to-one relationship , Including the main sub direction and the sub main direction , Plus one to many in an article , For one more , Many to many relationship , So far all the table relationships have been implemented .crudapi The system realizes the association between objects through configuration , The main sub table is realized without programming CRUD operation .

attach demo demonstration

This system belongs to the product level zero code platform , Unlike automatic code generators , You don't have to generate Controller、Service、Repository、Entity And so on , The program can be used as soon as it runs , real 0 Code , It can cover basic business independent CRUD RESTful API.

Official website address :https://crudapi.cn

Address of the test :https://demo.crudapi.cn/crudapi/login

原网站

版权声明
本文为[crudapi]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210821105037092o.html