Git Product home page Git Product logo

Comments (6)

jepiqueau avatar jepiqueau commented on June 1, 2024

@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.

jepiqueau avatar jepiqueau commented on June 1, 2024

@urbanboy see https://www.sqlite.org/omitted.html

from sqlite.

urbanboy avatar urbanboy commented on June 1, 2024

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.

jepiqueau avatar jepiqueau commented on June 1, 2024

@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.

urbanboy avatar urbanboy commented on June 1, 2024

Thank you so much, your sample code works well.Greetings.

from sqlite.

jepiqueau avatar jepiqueau commented on June 1, 2024

@urbanboy The support of FOREIGN KEY has been added in release [email protected]. Hope you will find it helpful

from sqlite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.