piccolo-orm/piccolo

LazyTableReference circular import

Opened this issue · 3 comments

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

@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.

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.

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.