DRF-ORM-off associated with the foreign key knowledge points (emphasis)

A base table

Defines the base table, the table is defined in the class:

class Meta:
    # 基表,为抽象表,是专门用来被继承,提供公有字段的,自身不会完成数据库迁移
    abstract = True

Second, table relationships

1, many: foreign key fields generally built in a multi-party,;

2-many: foreign key fields generally built in a high frequency one query;

3-one: To establish a foreign key field in an appropriate location based on actual demand;

Details of the table and the table of one to one, the author relies on the details of the tables, foreign key field generally built in the details of the table

Third, the foreign key field attribute

1.to management table, association table above requires quotes, use reflective association; it is recommended that you associate unquoted, no reflection, more extensive use of the association in the following table.

2.related_name foreign key field name set in reverse query: looking forward field names, reverse to look related_name value.

3.on_delete represents the foreign key field must be set to cascade to provide (value: models.CASCADE) Django1.xx in the system default, in Django2.x, CASAADE must manually clear the value.

4.db_constraint the foreign key table associated with the control, the default setting is represented disassociation True False.

Fourth, the off operation of the association table

Off the table relationships associated with:

General understanding: After creating tables and table relationships, and then using the db_constraint = False off the table associated, however, with a table for CRUD no effect. After switching off the table associated field can use the following four operations:

on_delete=modles.CASCADE ;

1.CASCADE ==> defaults cascade

2.DO_NOTHING ==> foreign key is not concatenated, it is assumed in Table A Table B dependent, record deletion B, Table A foreign key field without any treatment;

3.SET_DEFAULT ==> Table A dependency is assumed Table B, B records deleted foreign key field to A default value of the attribute table is set, it must be used with the default attribute;

4.SET_NULL ==> Table A dependency is assumed Table B, B records deleted foreign key field to Table A is null, it is necessary to use properties with null = True;

advantage:
1. does not affect the linked table query operation efficiency
Will increase with Table 2. "increase", "delete", "change" operating efficiency
3. Easy to reconstruct the late database table
Disadvantages:
Linking the database itself does not detect the operation table, prone dirty data, parameters need to avoid dirty data (dirty data management when necessary) by a strict logical

For example:

A dependent B, A first insertion recording, the recording of the record corresponding to B is not generated, in the case where there is no associated, this operation can be realized, but the data is dirty;

Then B is then added data, the dirty data to be processed. After the first turn operation B procedure A, satisfies more logical thinking, the same can be performed. The AB table by table query logic # even without any abnormal

Guess you like

Origin www.cnblogs.com/mqhpy/p/12103781.html