Foreign key in sqlite - sqlite

I am trying to convert the table that was dumped from mysql. Following is the code I have from mysql for creating the table:
CREATE TABLE "tbl_profession_attributes" (
"id" bigint(20) NOT NULL,
"tbl_profession_attribute_id" bigint(20) DEFAULT '0',
"code" varchar(10) NOT NULL,
"name" varchar(100) NOT NULL,
"keyword" text NOT NULL,
"tbl_profession_list_id" bigint(20) NOT NULL,
PRIMARY KEY ("id"),
KEY "tbl_passion_attribute_id" ("tbl_profession_attribute_id"),
KEY "tbl_passion_list_id" ("tbl_profession_list_id")
);
When I run this query for sqlite, I get the following error:
Query Error: near "KEY": syntax error Unable to execute statement
Can someone please help me resolve this.
Any help is much appreciated.

First, MySQL keyword KEY is synonym for INDEX. So this is not about foreign key at all.
Second, SQLite does not support creating non-primary index in CREATE TABLE statement. You should specify separate statement for create index, something like:
CREATE INDEX tbl_passion_attribute_id_idx
ON tbl_profession_attributes(tbl_profession_attribute_id)

Related

how to insert multiple foreign key in DB Browser for SQLite (GUI)

I am using SQLite and DB Browser for SQLite. I know that if you want to insert a foreign key using the GUI, you just have to scroll to the right and double click to insert the foreign key to the corresponding field. The problem is when you want to use a multiple foreign key. How or where should i put the multiple foreign key constrain using the GUI ?
my actual sql is (returns an exception 'foreign key mismatch - "diseases" referencing "beehives"...'):
CREATE TABLE "diseases" (
"id" INTEGER NOT NULL,
"id_beehive" INTEGER NOT NULL,
"id_apiary" INTEGER NOT NULL,
"disease" TEXT NOT NULL,
"treatment" TEXT NOT NULL,
"start_treat_date" DATE NOT NULL,
"end_treat_date" DATE,
PRIMARY KEY("id"),
FOREIGN KEY("id_beehive") REFERENCES "beehives"("number") ON UPDATE CASCADE,
FOREIGN KEY("id_apiary") REFERENCES "beehives"("id_apiary") ON UPDATE CASCADE
);
what i want to do using the gui is:
CREATE TABLE "diseases" (
"id" INTEGER NOT NULL,
"id_beehive" INTEGER NOT NULL,
"id_apiary" INTEGER NOT NULL,
"disease" TEXT NOT NULL,
"treatment" TEXT NOT NULL,
"start_treat_date" DATE NOT NULL,
"end_treat_date" DATE,
PRIMARY KEY("id"),
FOREIGN KEY("id_beehive","id_apiary") REFERENCES "beehives"("number","id_apiary") ON UPDATE CASCADE
);
beehives sql:
CREATE TABLE "beehives" (
"number" INTEGER NOT NULL,
"id_apiary" INTEGER NOT NULL DEFAULT -2,
"date" DATE,
"type" TEXT,
"favorite" BOOLEAN DEFAULT 'false',
PRIMARY KEY("number","id_apiary"),
FOREIGN KEY("id_apiary") REFERENCES "apiaries"("id") ON DELETE SET NULL
);
After researching, it is not possible for the moment.
Everytime you modify the structure of the table, what DB Browser for SQLite do on the background is renaming the table, creating a new table with the updated structure, move the data to this new table and delete the old table. So in the GUI it looks like its only updating.
Unfortunatly, to create a composite foreign key or modify a simple FK to a composite FK , you can only do it manualy from the SQL window.
example :
FOREIGN KEY("id_beehive","id_apiary") REFERENCES "beehives"("number","id_apiary") ON
UPDATE CASCADE

Sqlite3: Error deleting record: foreign key mismatch

I am trying to delete a record from table users.
Tried deleteing it with a DELETE-statement (DELETE FROM users WHERE user_id=10" as well as in my DB browser, but I get the above error, specifically it says: "Error deleting record: foreign key mismatch - "games" referencing "groups" (DELETE FROM "main"."users" WHERE rowid IN ('10');)".
Below my schema:
CREATE TABLE users (
user_id INTEGER PRIMARY KEY,
name VARCHAR(255) NOT NULL,
hash VARCHAR(255) NOT NULL,
UNIQUE(name));
CREATE TABLE 'groups' (
'group_name' VARCHAR(255) NOT NULL,
'turn' INTEGER NOT NULL,
'user_id'INTEGER,
FOREIGN KEY ('user_id') REFERENCES 'users'('user_id')
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE games (
game_id INTEGER PRIMARY KEY,
active INTEGER,
turn INTEGER,
group_name VARCHAR(255) NOT NULL,
FOREIGN KEY (turn) REFERENCES groups(turn_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (group_name) REFERENCES groups(group_name)
ON UPDATE CASCADE
ON DELETE NO ACTION
);
Why is this a problem? user_id is not even a foreign key in 'games'?? Thank you!
The problem in the end was my DB Browser for SQLite (3.11.2) whose GUI allows deletions but they don't actually work. When I tied again in the bash and closed and restarted the DB Browser, the rows were gone. Uninsightful but figured I'd post nonetheless in case anyone else comes across this.

sqlite integer primary key not null constraint failed

According to the SQLite documentation / FAQ a column declared INTEGER PRIMARY KEY will automatically get a value of +1 the highest of the column if omitted.
Using SQLite version 3.22.0 2018-01-22 18:45:57
Creating a table as follows:
CREATE TABLE test (
demo_id INTEGER PRIMARY KEY NOT NULL,
ttt VARCHAR(40) NOT NULL,
basic VARCHAR(25) NOT NULL,
name VARCHAR(255) NOT NULL,
UNIQUE(ttt, basic) ON CONFLICT ROLLBACK
) WITHOUT ROWID;
Then inserting like this:
INSERT INTO test (ttt, basic, name) VALUES ('foo', 'bar', 'This is
a test');
gives:
Error: NOT NULL constraint failed: test.demo_id
sqlite>
When it is expected to create a record with a demo_id value of 1. Even if the table already contains values, it'll fail inserting the row without explicitly specifying the id with the same error.
What am I doing wrong?
The documentation says that you get autoincrementing values for the rowid. But you specified WITHOUT ROWID.

generating .orm.yml file from a sql query

What is the best way to create entities (with configuration files) from available tables?
imagine that I have a create table query like this:
CREATE TABLE IF NOT EXISTS `mytable`.`task` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NOT NULL,
`description` TINYTEXT NOT NULL,
`due_date` DATETIME NOT NULL,
`attachment` TINYINT(1) NULL,
`project_id` INT NULL,
`user_id` INT NULL,
PRIMARY KEY (`id`),
INDEX `fk_task_1_idx` (`project_id` ASC),
INDEX `fk_task_2_idx` (`user_id` ASC),
CONSTRAINT `fk_task_1`
FOREIGN KEY (`project_id`)
REFERENCES `mava`.`project` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_task_2`
FOREIGN KEY (`user_id`)
REFERENCES `mava`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
is there any way that I can generate task.orm.yml file from this query.
I'm using symfony and doctrine and I have a bunch of tables defined already. So I need to generate Entities from them.
Thanks
Found the answer here:
How to Generate Entities from an Existing Database
Hope it helps someone else

SQLite: Constraint failed on Insert

I have one Auto Increment Field, rest are Integer,Text and Datetime field. How do I fix it out?
The Table Structure is given below:
CREATE TABLE "q1" (
"sb_id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"sb_title" text(100,0) NOT NULL,
"sb_details" text(300,0) NOT NULL,
"sb_image" text(30,0) NOT NULL,
"sb_type" integer(4,0) NOT NULL DEFAULT '1',
"sb_date" datetime NOT NULL
)
It could be because in your insert command
connection.execute("INSERT INTO q1(sb_title,sb_details)VALUES(?,?)",a,b);
you didn't insert any values for sb_image or sb_date, both of which are NOT NULL and have no default defined. SQLite doesn't know what to put in there. You should either take away the NOT NULL constraint on those columns, define a default for the columns, or insert something explicitly.

Resources