Returns SQL of the reference: ALTER TABLE "orders" ADD FOREIGN KEY ("customer_id") REFERENCES "customers ("id");
(model: Reference)
| 59 | |
| 60 | @DefaultSQLRenderer.renderer_for(Reference) |
| 61 | def render_reference(model: Reference) -> str: |
| 62 | ''' |
| 63 | Returns SQL of the reference: |
| 64 | |
| 65 | ALTER TABLE "orders" ADD FOREIGN KEY ("customer_id") REFERENCES "customers ("id"); |
| 66 | |
| 67 | ''' |
| 68 | validate_for_sql(model) |
| 69 | |
| 70 | if model.type == MANY_TO_MANY: |
| 71 | return generate_many_to_many_sql(model) |
| 72 | |
| 73 | result = '' |
| 74 | func = generate_inline_sql if model.inline else generate_not_inline_sql |
| 75 | if model.type in (MANY_TO_ONE, ONE_TO_ONE): |
| 76 | result = func(model=model, source_col=model.col1, ref_col=model.col2) |
| 77 | elif model.type == ONE_TO_MANY: |
| 78 | result = func(model=model, source_col=model.col2, ref_col=model.col1) |
| 79 | |
| 80 | c = f'CONSTRAINT "{model.name}" ' if model.name else '' |
| 81 | |
| 82 | return result.format(c=c) |