I have two tables, let’s call them property and address, plus some other tables.
Every property has a foreign key which references address, but other tables may also reference address. Not every address is associated with a property (for example, the address could be for a user).
I expect this to work, e.g. I can navigate to the address from the property.
Actual behavior
I get an error: Forest Admin tries to access address.propertyId (which doesn’t exist — not every address is associated with a property).
I believe this is because even though I only added the hasOne relationship to property, Forest Admin infers that addressbelongsTo the property, which is incorrect.
How should I change the model so that propertyhasOneaddress, but address does notbelongToproperty ?
Failure Logs
forest_admin_coral | [forest] 🌳🌳🌳 Unexpected error: column address.propertyId does not exist
forest_admin_coral | {
forest_admin_coral | "name": "SequelizeDatabaseError",
forest_admin_coral | "parent": {
forest_admin_coral | "length": 117,
forest_admin_coral | "name": "error",
forest_admin_coral | "severity": "ERROR",
forest_admin_coral | "code": "42703",
forest_admin_coral | "position": "154",
forest_admin_coral | "file": "parse_relation.c",
forest_admin_coral | "line": "3349",
forest_admin_coral | "routine": "errorMissingColumn",
forest_admin_coral | "sql": "SELECT count(\"property\".\"id\") AS \"count\" FROM \"franklin\".\"property\" AS \"property\" LEFT OUTER JOIN \"franklin\".\"address\" AS \"address\" ON \"property\".\"id\" = \"address\".\"propertyId\";"
Context
Please provide any relevant information about your setup.
Package Version: Not sure - docker-compose.yml has 3.4?
BelongTo and hasOne relationships are the same and just depend on the point of view from which one looks at the relationship. Can you describe the problem you had when the relationship was defined as belongsTo?
For the propertyId field, it is directly managed by the sequelize relations so it is normal if the field does not exist in your DB.
Thanks @Iclisson. So it sounds like you’re saying there’s no way to express a one-way foreign key relationship like I described?
For example, I have 3 tables: address, user, and property.
Both user and property hold an addressId. However, address contains neitheruserId nor propertyId (since there’s no guarantee which table is referencing the address), hence ‘one-way relationship’.
Within Forest Admin, I’d like to navigate to address from eitheruser or property. Is there alternative way to do this, if using hasOne + belongsTo doesn’t work?
BTW, this is a very common pattern in DB schema design, so it would be great if Forest Admin/Sequelize is able to accommodate this use case.
The behavior you observe is caused by Sequelize directly.
You might want to look into Polymorphic associations in Sequelize that seems to describe exactly your use-case.
The other solution I might think of is to create a smart relationship for your table instead of using the native one.
Both solutions should work fine, but with a polymorphic approach, you would be able to segregate the correct associated table if it is property or something else.
Please let me know if it helps you and if we can be of any more assistance.