tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.61k stars 383 forks source link

Foreign key between two apps #420

Closed filantus closed 4 years ago

filantus commented 4 years ago

Hi!

I have two postgres databases and two models sets (in different files). For expample: models.py and models_ext.py

I configure two connections and two apps:

{
                'connections': {
                    'default': {
                        'engine': 'tortoise.backends.asyncpg',
                        'credentials': {
                            'host': db.host,
                            'port': db.port,
                            'user': db.user,
                            'password': db.password,
                            'database': db.name,
                            'timeout': 10,
                        },
                    },
                    'ext': {
                        'engine': 'tortoise.backends.asyncpg',
                        'credentials': {
                            'host': db2.host,
                             'port': db2.port,
                            'user': db2.user,
                            'password': db2.password,
                            'database': db2.name,
                            'timeout': 10,
                        },
                    },
                },
                'apps': {
                    'models': {
                        'models': ['app.models'],
                        'default_connection': 'default',
                    },
                    'ext_models': {
                        'models': ['app.models_ext'],
                        'default_connection': 'ext',
                    }
                }
}

In models.py i try to create model with FK to model from models_ext.py:

models.py:

class Item(Model):
    ext_item = fields.ForeignKeyField('ext_models.ExtItem')

models_ext.py:

class ExtItem(Model):
    name = fields.CharField(max_length=255, unique=True)

It is even possible?

Now when i run this i get:

schema_generator.py", line 408, in get_create_schema_sql
    raise ConfigurationError("Can't create schema due to cyclic fk references")
tortoise.exceptions.ConfigurationError: Can't create schema due to cyclic fk references

OS: Win10x64

Packages versions: asyncpg==0.20.1 tortoise-orm==0.16.12

Postgres version: 12

grigi commented 4 years ago

Right now, no.

But I see your use case, you have a soft-relation between fields that can't enforce integrity.

We might have to introduce a new field type that would allow this "emulated fk/m2m" to another model on possibly another DB.

filantus commented 4 years ago

Thank you for answer!

Well, i realize what it can't have native constraints on dababase level, but just hoped for handy abilities (like underscored filters, fetching related objects, etc.) what will do routine work underhood.

Emulated Foreign Key - sounds very good. Eager to try it. When it'll be ready of course.

Thank you for great work!