简体   繁体   中英

SQLite and SQLite Net Extensions - where's the Foreign Key Constraint?

I am just confused here (maybe I am missing something?)

I've been pulling my hair out trying to work out why on earth my SQLite DB created using the sample on sqlite-net-extensions is not creating foreign key relationships

I do really truly appreciate the fact that people in the community take the time to package up, document, release and maintain their awesome code for the rest of us - I really do.

So thank you to all the contributors to SQLite, SQLiteNet and SQLite Net Extensions. Frank Krueger, Oystein Krog, Guillermo Gutiérrez Doral and all the rest.

However.... Erm, what !

I found the answer in this SO post

Yes, it is normal. SQLite-Net Extensions is built on top of SQLite-Net, that doesn't have support for foreign key constraints, so there's no foreign key declared at the database layer.

Using core SQLite-Net methods, the [ForeignKey] property is just an integer property that doesn't have any kind of restriction, so you can modify it at will without runtime errors.

ie the name of the ForeignKey attribute is very misleading. It's actually implemented as a simple Index

eg "Valuation_StockId" leaving you without data integrity constraint at the database level.

So you can (at the database level directly insert data that is non coherent, and not constrained) eg a Valuation referencing a Stock (via it's column StockId) that doesn't exist.

Not only that but nothing in the codebase stops you from doing the same.

So be careful folks the Attribute "ForeignKey" doesn't actually provide you with a Foreign Key relationship at all.

It is not a foreign key, and should not be named in a way to suggest it is - in my mind this is really dangerous .

Once again - thank you to the people who released all their code to the rest of us - and hopefully this might help someone else who like me is pulling their hair out trying to work out what they did wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM