Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relations using custom key #1789

Open
PROda221 opened this issue May 4, 2024 · 1 comment
Open

Relations using custom key #1789

PROda221 opened this issue May 4, 2024 · 1 comment

Comments

@PROda221
Copy link

PROda221 commented May 4, 2024

Hello everyone!

I want to create a simple 1 to 1 chat system where there are 2 models, Chat and Message.
Every chat can have many messages and each message belongs to only one chat.

My Chat Model:-

`import { Model } from '@nozbe/watermelondb';
import { date, field, readonly, children } from '@nozbe/watermelondb/decorators';

image

Message Model:-
image

Schema:- import {appSchema, tableSchema} from '@nozbe/watermelondb';
image

Setting chats with my custom "chat_id" is fine, but when I try to set messages with this chat_id it is not set inside _raw but on the Message object itself...

Using this function to create new messages:-

image

Response I am getting when new message is created:-
{"__changes": {"_value": [Circular], "closed": false, "currentObservers": null, "hasError": false, "isStopped": false, "observers": [], "thrownError": null}, "_isEditing": false, "_preparedState": null, "_raw": {"_changed": "", "_status": "created", "chat_id": "", "created_at": 1714842531304, "id": "zxNLPlK8CSJ1cYwB", "is_received": false, "text": "dddd", "updated_at": 1714842531304}, "_relationCache": {"chat": {"_columnName": "chat_id", "_isImmutable": false, "_model": [Circular], "_relationTableName": "chats"}}, "_subscribers": [], "collection": {"_cache": {"_debugCollection": [Circular], "map": [Map], "recordInsantiator": [Function anonymous], "tableName": "messages"}, "_subscribers": [], "changes": {"closed": false, "currentObservers": [Array], "hasError": false, "isStopped": false, "observers": [Array], "thrownError": null}, "database": {"_isBeingReset": false, "_isBroken": false, "_pendingNotificationBatches": 0, "_pendingNotificationChanges": [Array], "_resetCount": 0, "_subscribers": [Array], "_workQueue": [WorkQueue], "adapter": [DatabaseAdapterCompat], "collections": [CollectionMap], "schema": [Object]}, "modelClass": [Function Message]}}

Any idea!?? I am fairly new to this.

@primus11
Copy link

primus11 commented May 6, 2024

Each model/table will automatically get id. When defining relations it is this automatic id that is used. So you should probably lose your custom chat_id. Check also Relations where in first example your Message is really Comment and your Chat is User.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants