简体   繁体   中英

Choosing a relationship between two tables (SQLAlchemy)

Consider two tables: User and Calculation.

Calculation's structure: {“id”: <primary_key>, “array”: <array of numbers>, “calculations”: <array of all calculations for given array of numbers>}

User creates those calculations.

I want to be able to query for calculations associated with specific User. As far as I know, I need to have user reference on calculation model to do that.

Question is: is it possible to avoid changing Calculation structure by having User reference on Calculation model?

I was thinking about having an array field on User model with IDs of calculations. Is this a viable solution?

You can use ArrayField . You should create this field in your User model and you can fill it with IDs of Calculations. It would propably looks like this:

from django.contrib.postgres.fields import ArrayField
from django.db import models

class User(models.Model):
    calculations = ArrayField(models.IntegerField(blank=True),blank=True)

Of course you can change blank options, i wrote it just to show how you can use this for both fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM