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

[BUG]. Using string reference for Links is not possible #1035

Open
Yalchin403 opened this issue Sep 30, 2024 · 2 comments
Open

[BUG]. Using string reference for Links is not possible #1035

Yalchin403 opened this issue Sep 30, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Yalchin403
Copy link

Describe the bug
Unable to use string references with Link across different modules. When using string references for Link without importing the actual model classes (i.e., relying on forward references), a ForwardRef error is raised. This issue arises when models from different modules reference each other.

To Reproduce
Provide the following code snippet to reproduce the issue:

# users/models.py
from beanie import Document, Link
from pydantic import Field
from typing import Optional

class User(Document):
    username: str = Field(unique=True, index=True)
    email: str
    bio: Optional[str] = None

    class Settings:
        name = "users"

class Profile(Document):
    user: Link[User]
    avatar: Optional[str] = None
    favorite_post: Optional[Link['Post']] = None  # Forward reference

    class Settings:
        name = "profiles"

# posts/models.py
from beanie import Document, Link
from pydantic import Field
from typing import List
from datetime import datetime

class Post(Document):
    title: str
    content: str
    author: Link['User']  # Forward reference
    created_at: datetime = Field(default_factory=datetime.utcnow)

    class Settings:
        name = "posts"

class Comment(Document):
    post: Link[Post]
    author: Link['User']  # Forward reference
    content: str
    created_at: datetime = Field(default_factory=datetime.utcnow)

    class Settings:
        name = "comments"

Expected behavior
When there are no circular relationships in the database schema, I expect to be able to reference models from different modules using string references (without importing them explicitly) without encountering ForwardRef errors.

Additional context
This error occurs when defining Link references across modules. Ideally, forward references should allow linking models without circular imports. This behavior is necessary when defining related models in separate files.

@marconadar
Copy link

I don't have a solution, but I’m interested in how you fetch a Comment or Post, and whether the User is populated. When I fetch, for example, a Comment, I only get the ObjectId of the User or Post, but not the actual data from the Post or User document.
I have the feeling that find with fetch_links=True is not working correctly.

Copy link
Contributor

github-actions bot commented Nov 4, 2024

This issue is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the Stale label Nov 4, 2024
@staticxterm staticxterm added bug Something isn't working and removed Stale labels Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants