I'm working with an Account model and I want to inner join it with a Settings model without having to create an additional settings_id
column in my Account model, because the PK on the Account table matches the PK on the settings table exactly. I've been trying to set up a OneToOne relationship between these tables & columns, but can't figure out how to do that without having to create a new column or something. I'm trying to work with the existing schema as much as possible and would prefer to not create unnecessary columns.
Class Account(models.Model):
login_id = models.AutoField(primary_key=True)
settings = models.OneToOneField('Settings', to_field='login_id', null=True,
related_name='settings', db_column='login_id') # Doesn't work because login_id already exists....
Class Settings(model.Model):
login_id = models.OneToOneField('Account', to_field='login_id', related_name='account')
Basically, the query I'm trying to replicate is:
SELECT * FROM account
INNER JOIN settings
ON account.login_id=settings.login_id
WHERE account.login_id=1
Based on the errors, Django's ORM seems really persistent on creating a new column in the Account table, but I don't see the point of adding a new column when the relationship between the 2 tables is so simple: account.login_id = settings.login_id