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

How to reference a Table which is in another package? #2871

Closed
sun-jiao opened this issue Jan 30, 2024 · 5 comments
Closed

How to reference a Table which is in another package? #2871

sun-jiao opened this issue Jan 30, 2024 · 5 comments

Comments

@sun-jiao
Copy link

sun-jiao commented Jan 30, 2024

Describe the bug

When Tables and the Database are in different packages, build always fails with the following warning:

[WARNING] drift_dev on lib/db/drift_birdart.dart:
line 14, column 7 of package:birdart/db/drift_birdart.dart: The referenced element, BirdTable, is not understood by drift.
   ╷
14 │ class AppDatabase extends _$AppDatabase {
   │       ^^^^^^^^^^^
   ╵

When the Tables are placed in the same package, the build will success.

@sun-jiao sun-jiao changed the title How to reference a Table in another file? How to reference a Table which is in another file? Jan 30, 2024
@sun-jiao sun-jiao changed the title How to reference a Table which is in another file? How to reference a Table which is in another package? Jan 30, 2024
@sun-jiao
Copy link
Author

sun-jiao commented Jan 30, 2024

Let me explain why I want to do this: I'm writing a dart full stack application (meaning shelf back-end, flutter mobile app and flutter web) and I want to place the shared code - such as the data model definition - in a library, then import them in the shelf project and flutter project. However I realized that drift doesn't seem to allow it. I'm currently using floor and trying drift for the first time today, because drift supports not only sqlite but also mysql or postgresql - obviously, it's not a good idea to use sqlite on the backend.

@simolus3
Copy link
Owner

This is something that should generally work, but requires a bit of setup. The package containing BirdTable needs to have a public (so non-dev) dependency on drift_dev so that drift's analyzing builder can run on those sources.

and I want to place the shared code - such as the data model definition - in a library, then import them in the shelf project and flutter project

One thing worth mentioning is that drift's generator is monolithic by default - it generates all the code into a single part file, the one for the database. So all the generated table classes and so on would be duplicated if both the server and the client are declaring a drift database. A solution is to use modular builds, in which drift will generate multiple files and actually allow sharing data classes.

@sun-jiao
Copy link
Author

Thanks. However, adding drift_dev does not fix it.

I solved the problem by moving the database class to the shared package.

@simolus3
Copy link
Owner

I've also pushed an example using two different database packages sharing some definitions in af55bd0.

@sun-jiao
Copy link
Author

@simolus3 thanks. I did not run builder in the shared package.

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