generating .orm.yml file from a sql query - symfony

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

Related

Cannot restart Strapi after making changes to Content Types (using SQLite)

I have followed the quickstart setup for Strapi, which as I understand sets up an SQLite Database. I created one collection type (painting) which worked without problems, but whenever I try to add a new one or make changes to the existing one the server does not restart and I receive the following error output in my console:
[2022-05-23 16:53:16.323] error: CREATE TABLE _knex_temp_alter889 (id integer not null primary key autoincrement PRIMARY KEY AUTOINCREMENT NOT NULL, title varchar(255) NULL, artist varchar(255) NULL, created_at datetime NULL, updated_at datetime NULL, published_at datetime NULL, created_by_id integer NULL, updated_by_id integer NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL) - table "_knex_temp_alter889" has more than one primary key
SqliteError: CREATE TABLE _knex_temp_alter889 (id integer not null primary key autoincrement PRIMARY KEY AUTOINCREMENT NOT NULL, title varchar(255) NULL, artist varchar(255) NULL, created_at datetime NULL, updated_at datetime NULL, published_at datetime NULL, created_by_id integer NULL, updated_by_id integer NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL) - table "_knex_temp_alter889" has more than one primary key
Deleting the changes I made in my content-types files is currently the only way I have of getting the server to run again. How do I fix this?
So, I ended up creating a new project. Problem seems to have been that I added a field named 'ID', which I assume messed everything up because it conflicted with the automatically assigned ID-System.

Strange org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (foreign key mismatch -

I'm running into org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (foreign key mismatch - ... with a statement, that proceeds without any complaints using the normal SQLite-frontend. This creates the crucial part of my database:
CREATE TABLE IF NOT EXISTS artists (
aid INTEGER PRIMARY KEY AUTOINCREMENT,
aname VARCHAR(200) NOT NULL,
CONSTRAINT one UNIQUE(aname)
);
CREATE TABLE IF NOT EXISTS discs (
did INTEGER PRIMARY KEY AUTOINCREMENT,
testAddCD1 BIGINT(10) NOT NULL,
dtitle VARCHAR(125) NOT NULL,
dreleaseyear YEAR(4) DEFAULT NULL,
dlang VARCHAR(3) DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS tracks (
discs_did INTEGER NOT NULL,
tnumber INT(4) NOT NULL,
ttitle VARCHAR(125) NOT NULL,
tseconds INT(4) NOT NULL,
CONSTRAINT pk PRIMARY KEY(discs_did, tnumber),
CONSTRAINT fk FOREIGN KEY(discs_did) REFERENCES discs(did) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT val CHECK(tseconds> 0)
);
CREATE TABLE IF NOT EXISTS track_by_artist (
discs_did INTEGER NOT NULL,
tracks_tnumber INT(4) NOT NULL,
artists_aid INTEGER NOT NULL,
CONSTRAINT pk PRIMARY KEY(discs_did, tracks_tnumber, artists_aid),
CONSTRAINT fk1 FOREIGN KEY(discs_did) REFERENCES discs(did) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT fk2 FOREIGN KEY(tracks_tnumber) REFERENCES tracks(tnumber) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT fk3 FOREIGN KEY(artists_aid) REFERENCES artists(aid) ON DELETE RESTRICT ON UPDATE RESTRICT
The database gets created and the JDBC-driver inserts an artist, a disc and the disc's tracks - all good. The final insert should assign an artist to a track and looks like
INSERT INTO track_by_artist (discs_did, tracks_tnumber, artists_aid) VALUES (1, 1, 1);
Using the JDBC this yields
SQLite-Error #1
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (foreign key mismatch - "track_by_artist" referencing "tracks")
at org.sqlite.core.DB.newSQLException(DB.java:1012)
at org.sqlite.core.DB.newSQLException(DB.java:1024)
at org.sqlite.core.DB.throwex(DB.java:989)
at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
at org.sqlite.core.NativeDB.prepare(NativeDB.java:134)
at org.sqlite.core.DB.prepare(DB.java:257)
at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:45)
at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:25)
at org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:35)
at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:241)
at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:205)
Issuing the same SQL-Insert with SQLite's text-frontend works like cream.
I'm a little lost and don't know what to do about my Java-code.
Some advise, pls?
Chris
The problem is that in track_by_artist you defined this foreign key constraint:
CONSTRAINT fk2 FOREIGN KEY(tracks_tnumber) REFERENCES tracks(tnumber) ON DELETE RESTRICT ON UPDATE RESTRICT
although tnumber in tracks is not UNIQUE (and it shouldn't be).
A foreign key's parent must be defined as UNIQUE.
In tracks the PRIMARY KEY is defined as the combination of discs_did and tnumber, which makes sense, so the combination of these 2 columns is unique.
What you can do is define in track_by_artist a composite foreign key for the columns discs_did and tracks_tnumber that reference discs_did and tnumber of tracks:
CREATE TABLE IF NOT EXISTS track_by_artist (
discs_did INTEGER NOT NULL,
tracks_tnumber INT(4) NOT NULL,
artists_aid INTEGER NOT NULL,
CONSTRAINT pk PRIMARY KEY(discs_did, tracks_tnumber, artists_aid),
CONSTRAINT fk1 FOREIGN KEY(discs_did, tracks_tnumber) REFERENCES tracks(discs_did, tnumber) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT fk2 FOREIGN KEY(artists_aid) REFERENCES artists(aid) ON DELETE RESTRICT ON UPDATE RESTRICT
);
This way you don't need a separate foreign key definition for discs_did.

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.

Recursive foreign key 'on delete cascade'

CREATE TABLE IF NOT EXISTS type (
tid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (tid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_type_0 ON type (tid,uuid,name);
CREATE TABLE IF NOT EXISTS object (
oid VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
tid INTEGER NOT NULL,
CONSTRAINT FK_tid FOREIGN KEY (tid) REFERENCES type(tid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid)
);
CREATE INDEX IDX_object_0 ON object (oid,timestamp,tid);
CREATE TABLE IF NOT EXISTS object_user_owner (
uid INTEGER NOT NULL,
oid VARCHAR NOT NULL,
CONSTRAINT FK_uid FOREIGN KEY (uid) REFERENCES user(uid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT FK_oid FOREIGN KEY (oid) REFERENCES object(oid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid,uid)
);
CREATE INDEX IDX_object_user_owner_0 ON object_user_owner (uid,oid);
CREATE TABLE IF NOT EXISTS user (
uid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
password VARCHAR NOT NULL,
salt VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
lastaccess VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (uid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_user_0 ON user (uid,uuid,name);
The above three sqlite3 tables contain foreign keys. The problem is the deletion of a key in the upper table type. When I try do delete I get 'FOREIGN KEY constraint failed' Error. Deleting a from the lowest table object_user_owner before deleting a type works. I think sqlite does not check any recursive cascade constraints. Does anyone have experienced this too or is anything wrong with my design?

Foreign Key constraint doesn't work

I have two tables, theme and quiz, here is their definition:
CREATE TABLE "theme" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "nom" VARCHAR NOT NULL );
CREATE TABLE quiz(
id INTEGER PRIMARY KEY,
nom VARCHAR(256) NOT NULL,
theme INTEGER NOT NULL,
niveau INTEGER NOT NULL,
pass INTEGER DEFAULT 1 NOT NULL,
jok INTEGER DEFAULT 1 NOT NULL,
etat INTEGER DEFAULT 0 NOT NULL,
FOREIGN KEY (theme) REFERENCES theme(id)
);
The field id (the primary key) in the table theme is a Foreign Key in the quiz table.
When i try to insert a record in the table quiz which contain the value 30 for example as a foreign key, the record is inserted successfully in the quiz table although there is no record in the theme table with the id = 30, i mean, wasn't supposed to interdict this insert since i had a Foreign key constraint?
Are you sure foreign key support is enabled?
Assuming the library is compiled with foreign key constraints enabled,
it must still be enabled by the application at runtime, using the
PRAGMA foreign_keys command. For example:
sqlite> PRAGMA foreign_keys = ON;

Resources