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

LazyTableReference circular import #935

Open
Abdelhadi92 opened this issue Feb 28, 2024 · 3 comments
Open

LazyTableReference circular import #935

Abdelhadi92 opened this issue Feb 28, 2024 · 3 comments

Comments

@Abdelhadi92
Copy link

Abdelhadi92 commented Feb 28, 2024

I'm facing circular import when I use LazyTableReference with app_name, please check the attached video below:

screen-recording-2024-02-28-at-11029-pm_zxUcfsGD.mp4
@sinisaos
Copy link
Member

@Abdelhadi92 You are right. LazyTableReference doesn't work with app_name argument, but it works with module_path like this

from piccolo.table import Table
from piccolo.columns import BigSerial, ForeignKey, LazyTableReference

class Limit(Table):
    id = BigSerial(primary_key=True)
    country = ForeignKey(
        references=LazyTableReference(
            table_class_name="Country",
            module_path="src.modules.compliance.tables",
        )
    )

I actually don't understand why we have both arguments because they serve the same purpose (just my opinion, sorry if I'm wrong) and I think module_path is enough. I also noticed that after the migrations were created in the wallet module, the Varchar column was missing from migration file, but this is easily solved by adding a Varchar column like this

from piccolo.columns.column_types import Varchar

to the migrations. After that I ran migrations and everything was fine.
In any case, I think you should use LazyTableReference as little as possible (see this discussion) and you should always try to organize schemas, folders and files so that you can directly use tables from another modules like this

from piccolo.table import Table
from piccolo.columns import BigSerial, ForeignKey, LazyTableReference
from src.modules.compliance.tables import Country

class Limit(Table):
    id = BigSerial(primary_key=True)
    country = ForeignKey(references=Country)

I think that with this way of defining ForeignKey, there is less possibility of circular import and other errors. Sorry for long comment.

@Abdelhadi92
Copy link
Author

Hi @sinisaos, thank you for your response.

Regarding import the Table class directly I don't think this will fix the issue if you have foreign key from first to the second app and another one from the first to the second

from piccolo.table import Table
from piccolo.columns import BigSerial, ForeignKey, LazyTableReference
from src.modules.compliance.tables import Country

class Limit(Table):
    id = BigSerial(primary_key=True)
    country = ForeignKey(references=Country)

Piccolo Finder not designed in the right way ... piccolo_conf importing the apps (tables classes) and the LazyTableReference in the table trying to import the piccolo_conf

Anyhow, module_path is fixing my issue but to be honest this my first project on Piccolo and I found critical 2 bugs in 2 days so i'm concerned about use it.

@sinisaos
Copy link
Member

Anyhow, module_path is fixing my issue but to be honest this my first project on Piccolo and I found critical 2 bugs in 2 days so i'm concerned about use it.

I'm sorry to hear that. I will be glad if I can help somehow.

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