Comments (13)
For anyone else that needs this, you can check out my fork:
https://github.com/EmileSpecs/loopback-connector-mysql/tree/feature/foreign-key-migration
It just supports "belongsTo" relations at the moment since those are all I needed. Shouldn't be to difficult to add other relevant relationship types.
It makes sense to me that the database takes care of relational integrity of data and it saves a lot of code to not have to worry about cascading your deletions to linked models.
from loopback-connector-mysql.
Since I use automigrate a lot for my tests, I've added the following code after automigrate to add indexes after table creation
let sql = 'ALTER TABLE quote add constraint fk_book foreign key (book_id) REFERENCES book (id);'
Quote.dataSource.connector.query(sql, done);
from loopback-connector-mysql.
Hi, @fusionstrings.
The automigrate/autoupdate doesn't support relations at this moment.
from loopback-connector-mysql.
ohh ok, any work around then?
from loopback-connector-mysql.
Please note that LoopBack's relation is not tied to RDB foreign keys. It doesn't use SQL JOIN. For example, to query all transactions for a given account, say /api/accounts//transactions, LoopBack generates a query like:
select * from transaction where accountId = .
Can you elaborate why you need to set up the RDB foreign keys? Is it for referential integrity?
from loopback-connector-mysql.
Exactly @raymondfeng , I require to use foreign keys for referential integrity. I understand LoopBack uses "WHERE" query and as a work around I am adding foreign keys directly in database but while running automigrate or autoupdate, it throws some error due to that, although it works fine.
As a temporary workaround I am thinking about not to add foreign keys constraint in database until model definitions are stable. Once need to run autoupdate/automigrate is eliminated, I can manually add foreign keys. Somehow it does not feel right.
from loopback-connector-mysql.
The autoupdate/automigrate is only required if your model changes. You should be able to manually add foreign keys after autoupdate/automigrate is done and disable autoupdate/automigrate.
from loopback-connector-mysql.
This helps a lot and I am glad I was thinking in right direction. Thanks a lot.
from loopback-connector-mysql.
Hi @fusionstrings , just stumble upon this same problem and I was wondering "what am I doing wrong?" but yes, it appears that loopback is not adding the foreign key constrains in the RDB on migrate. I wanted to ask you: what approach did you finally use? did you add the constrains yourself manually? I'm finding very cumbersome having to add extra logic in the models controller to add the proper reference to the owner model id
from loopback-connector-mysql.
@josefd8 Yes it's cumbersome but that's the workaround I'd to adopt. As of not it's been ages since I'd used loopback so can't say much.
from loopback-connector-mysql.
This can help:
https://www.npmjs.com/package/loopback-mysql-referential-integrity-component
from loopback-connector-mysql.
This is supported by the newer mysql-connector. The information is buried in the documentation (specifically here). See also discussion #294.
from loopback-connector-mysql.
Does it have any impact on performance when doing complex queries ?
from loopback-connector-mysql.
Related Issues (20)
- Support for Full Text Search
- AWS EC2 LoopBack Keep Connecting to 127.0.0.1 Even Already Specified RDS Endpoint HOT 6
- Tests are failing HOT 3
- Default Function UUID inconsistence HOT 1
- Connection to database hangs after Error: read ETIMEDOUT HOT 2
- Model's ID not set as private key in MySQL database
- RAW Query fails when server and DB are on differents IPs
- Fulltext index on a model property is not migrated to the datasource
- mysql.index is not work on property
- compatibility issue with mysql aurora 8.0.* using regex HOT 1
- n-gram specification on FULLTEXT index HOT 2
- Mapping of type blob/longblob to Binary fails the lb4 discover command HOT 1
- Fix CI pipeline HOT 1
- FULLTEXT | MYSQL | failed with status code 500. Error: ER_PARSE_ERROR: You have an error in your SQL syntax; HOT 1
- Add support for nodejs v18 and mysql v8 HOT 5
- Empty body on update generates an MySQL error HOT 2
- Support Latest Node 18 (LTS) HOT 1
- Dependency Dashboard
- Remove unknown options to the mysql2 initalizer HOT 3
- empty string is converted to empty object in LoopBack3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loopback-connector-mysql.