Comments (6)
@urbanboy first thanks to use the plugin. if i understand well your question, as you didn't give an example of the SQL statement you are trying to execute, this is happening when you are trying to ALTER a table to add a FOREIGN KEY and to add a REFERENCES to the key of another table.
ALTER TABLE child ADD CONSTRAINT fk_child_parent
FOREIGN KEY (parent_id)
REFERENCES parent(id);
If it is the case, this is not supported by sqlite and de facto by the plugin. What you have to do is to create the FOREIGN KEY and the REFERENCES at the time you CREATE the table.
CREATE TABLE child (
id INTEGER PRIMARY KEY,
parent_id INTEGER,
description TEXT,
FOREIGN KEY (parent_id) REFERENCES parent(id)
);
Unfortunately, if your table already exists, either you will loose the data or you have to copy your_table into child
INSERT INTO child (id,description) SELECT id,description FROM your_table;
and after this update for each row of child the parent_id, drop your_table and rename child to your_table.
Voilà, hope it is a clear answer
from sqlite.
@urbanboy see https://www.sqlite.org/omitted.html
from sqlite.
Thanks You, definetly I ever trying add foreing key at creation of table like you but is imposible table is incomplet without the column of foreign key, maybe is posible use the documentation about PRAGMA foreign_keys ? https://www.sqlite.org/foreignkeys.html
Sentence of creation of table with foreign key not works just un the line of definition of foreing key, Can You try this and then use of exportToJson method's plugin and check that output of Json of database with Foreign key?
Cheers.
from sqlite.
@urbanboy
i did this and it works fine
// create tables
let sqlcmd: string = `
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY NOT NULL,
email TEXT UNIQUE NOT NULL,
name TEXT,
company TEXT,
size FLOAT,
age INTEGER
);
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY NOT NULL,
userid INTEGER,
title TEXT NOT NULL,
body TEXT NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id)
);
CREATE INDEX users_index_name ON users (name);
`;
result = await this._SQLiteService.execute(sqlcmd);
console.log('1 result.changes')
if(result.changes.changes === 0 || result.changes.changes === 1) {
sqlcmd = `
BEGIN TRANSACTION;
INSERT INTO users (name,email,age) VALUES ("Simpson","[email protected]",69);
INSERT INTO users (name,email,age) VALUES ("Jones","[email protected]",42);
INSERT INTO messages (id,userid,title,body) VALUES (1,1,"Message 1","Blabla");
INSERT INTO messages (id,userid,title,body) VALUES (2,1,"Message 2","More Blabla");
INSERT INTO messages (id,userid,title,body) VALUES (3,2,"Message 1","Response from Blabla");
COMMIT TRANSACTION;
`;
result = await this._SQLiteService.execute(sqlcmd);
console.log('2 result.changes')
if(result.changes.changes === 5) {
}
}
Now for the importFromJson and exportToJson i will have to see how it can be implemented
Hope this will help
from sqlite.
Thank you so much, your sample code works well.Greetings.
from sqlite.
@urbanboy The support of FOREIGN KEY has been added in release [email protected]. Hope you will find it helpful
from sqlite.
Related Issues (20)
- Backup/restore solution HOT 5
- Double create connection HOT 2
- execute method in sqliteconnection does not work when sending a delete query in ios alone but works in android. (ionic angular app ) HOT 4
- Attempt to insert values before table is created HOT 6
- Export / backup db binary file on Android device? HOT 8
- importFromJson on android fails to build SQL Statement with `'` in values. HOT 19
- Error CapacitorSQLitePlugin :null only in production HOT 24
- ImportFromJSON, delete only works for integer primary keys HOT 4
- addSQLiteSuffix or moveDatabasesAndAddSuffix doesn't copy data inside the original database HOT 22
- ImportFromJson not working HOT 10
- Request: Add examples for angular and sqlite without Ionic. HOT 7
- Support API level 34 on Android HOT 2
- Support for dynamic database path HOT 1
- exportToJson getSchema Exception HOT 10
- Electron Vite: Error resolving capacitor.config.js
- The run method returns the wrong type HOT 3
- Outdated Ionic React + TypeORM repo HOT 2
- Error: The jeep-sqlite element is not present in the DOM when trying to generate migration files via TypeOrm CLI HOT 29
- out of memory since 1.9.0 HOT 8
- ZIPFoundation vulnerability HOT 1
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 sqlite.