Zero code to achieve one-to-one table relationship and unlimited main and sub-table cascade storage

Table relationship high-level

In the previous table relationship management , the introduction of order-to-many, many-to, and many relationships, this article describes the application of one relationship and unlimited master table in crudapi system.

Overview

One to one

A one-to-one relationship refers to a relationship between two tables in a relational database. A single row in the first table in a relational database can only be related to one row in the second table, and a row in the second table can only be related to one row in the first table.
In a one-to-many relationship, the foreign key is established in the sub-table; in the one-to-one relationship, the foreign key can be built in the main table or the sub-table. In order to maintain consistency, the crudapi system unified the foreign key in the one-to-one relationship. Established in the child table, the advantage of this is that if the table relationship needs to be lifted in the future, there is no need to modify the main table structure.

Customer Profile

The customer master table usually stores basic information. If there are more information, you can consider storing them separately in the customerProfile table, and they can be implemented in a one-to-one relationship.

Customer and customer information: one-to-one (master and sub-direction)

customer
The customer table created before remains unchanged

customerProfile
Create the customerProfile table of customer profile, which mainly includes customer number fields, birthdays, gender, hobbies, etc. The customerId field of customer number is used to establish the table relationship

profileRelation
Establish a one-to-one relationship between the master and the child. The number id field of the customer table points to the customer ID customerId field of the customer profile customerProfile. The English name profile of the relationship is used to query the associated object, and the navigation property name is set to profile.

Customer profile and customer: one-to-one (sub-master direction)

customerRelation
Establish a one-to-one relationship between the child and the main direction. The customer ID field of the customer profile customerProfile points to the number id field of the customer table. When the English name of the relationship is used to query the associated object, set the navigation property name to customer, and the previous order Similar to the many-to-one relationship between customers, the associated objects are all customer objects, but at that time multiple orders can be associated with the same customer, and now a customer profile can only be associated with one customer.

Cascading storage of customer master and sub-tables

createCustomer
ui creates a customer, and enters the customer profile information in the sub-table at the same time, chrome opens the network request record, you can see the POST body as follows:

{
    "name": "刘备",
    "mobile": "13699998888",
    "email": "[email protected]",
    "profile": {
        "name": "刘备资料",
        "birthday": "2021-02-14",
        "sex": "男",
        "hobby": "骑马"
    }
}

Where profile is the customer profile information,

editCustomer
Inquiry about the customer details, it was found that the customer and customer profile information was successfully saved at one time! In the previous article, the sales order and the order line are one-to-many master-
child relationships, and the child tables are in the form of arrays. In the one-to-one master-child relationship, the child tables are in the form of objects. The form of expression in the database is the same, and the foreign keys are all Built in the child table.

Directly manipulate customer data

editCustomerProfile
You can also directly create a customer information table and choose to hang it under a designated customer to achieve the same effect.

Infinite child table

By setting the table relationship, one-to-many and one-to-one (primary and subdirection) can theoretically be associated indefinitely, and all tables are stored in cascade at one time. For example, provinces and municipalities can usually reach level 3 sub-tables, and directory files belong to unlimited sub-tables. .

Provincial and municipal three-level sub-table

ssqGraph

Provinces and cities have a one-to-many relationship, and cities and districts have a one-to-many relationship.

ssq
ui effect, Jiangsu Province includes two cities, Nanjing and Huai'an, and Nanjing City includes Jiangning District and Yuhuatai District.

Directory unlimited sublists

The first-level directory can include sub-directories and files. If it is a sub-directory, the sub-directories can continue to include sub-directories and file
folder1
map directories. Unlimited sub-tables-1

folder2
Figure directory unlimited sub-table-2

The ui effect, the sub-tables include directories and files, and can continue to be expanded. Due to the size of the screen, they will not be shown here.

Query directory listing
folders

summary

This article introduces the one-to-one relationship, including the master-subdirection and the sub-master direction, plus one-to-many, many-to-one, and many-to-many relationships in an article. So far all the table relationships have been realized. The crudapi system realizes the association between objects through configuration, and realizes the CRUD operation of the main and sub-tables without programming.

Attached demo

This system belongs to a product-level zero-code platform. It is different from an automatic code generator. It does not need to generate Controller, Service, Repository, Entity and other business codes. It can be used when the program is running. The real zero code can cover basic and business-unrelated codes. CRUD RESTful API.

Official website address:https://crudapi.cn
test address:https://demo.crudapi.cn/crudapi/login

Guess you like

Origin blog.51cto.com/15149911/2678946