When I create tables for my entities, Doctrine creates auth."user" table no problem:
[vagrant#localhost thiriscart]$ ./bin/console doctrine:schema:update --force --dump-sql
CREATE SCHEMA auth;
CREATE SCHEMA catalog;
CREATE SEQUENCE auth.user_id_seq INCREMENT BY 1 MINVALUE 1 START 1;
CREATE SEQUENCE catalog.category_id_seq INCREMENT BY 1 MINVALUE 1 START 1;
CREATE SEQUENCE catalog.product_id_seq INCREMENT BY 1 MINVALUE 1 START 1;
CREATE TABLE auth."user" (id INT NOT NULL, username VARCHAR(255) NOT NULL, PRIMARY KEY(id));
CREATE TABLE catalog.category (id INT NOT NULL, parent_id INT DEFAULT NULL, created_by INT NOT NULL, updated_by INT NOT NULL, title VARCHAR(255) NOT NULL, description TEXT NOT NULL, slug VARCHAR(255) NOT NULL, lft INT NOT NULL, rgt INT NOT NULL, root INT DEFAULT NULL, lvl INT NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id));
CREATE INDEX IDX_D3431049727ACA70 ON catalog.category (parent_id);
CREATE INDEX IDX_D3431049DE12AB56 ON catalog.category (created_by);
CREATE INDEX IDX_D343104916FE72E1 ON catalog.category (updated_by);
CREATE TABLE catalog.product (id INT NOT NULL, main_category INT NOT NULL, title VARCHAR(255) NOT NULL, digest TEXT NOT NULL, description TEXT NOT NULL, base_price NUMERIC(10, 2) NOT NULL, PRIMARY KEY(id));
CREATE INDEX IDX_BC02688FDF6E08B4 ON catalog.product (main_category);
ALTER TABLE catalog.category ADD CONSTRAINT FK_D3431049727ACA70 FOREIGN KEY (parent_id) REFERENCES catalog.category (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE catalog.category ADD CONSTRAINT FK_D3431049DE12AB56 FOREIGN KEY (created_by) REFERENCES auth."user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE catalog.category ADD CONSTRAINT FK_D343104916FE72E1 FOREIGN KEY (updated_by) REFERENCES auth."user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE catalog.product ADD CONSTRAINT FK_BC02688FDF6E08B4 FOREIGN KEY (main_category) REFERENCES catalog.category (id) NOT DEFERRABLE INITIALLY IMMEDIATE;
Updating database schema...
Database schema updated successfully! "16" queries were executed
But doesn't drop it:
[vagrant#localhost thiriscart]$ ./bin/console doctrine:schema:drop --force --dump-sql
ALTER TABLE catalog.category DROP CONSTRAINT fk_d3431049727aca70;
ALTER TABLE catalog.category DROP CONSTRAINT fk_d3431049de12ab56;
ALTER TABLE catalog.category DROP CONSTRAINT fk_d343104916fe72e1;
ALTER TABLE catalog.product DROP CONSTRAINT fk_bc02688fdf6e08b4;
DROP SEQUENCE auth.user_id_seq CASCADE;
DROP SEQUENCE catalog.category_id_seq CASCADE;
DROP SEQUENCE catalog.product_id_seq CASCADE;
DROP SEQUENCE ext_log_entries_id_seq CASCADE;
DROP SEQUENCE auth.user_id_seq CASCADE;
DROP SEQUENCE catalog.category_id_seq CASCADE;
DROP SEQUENCE catalog.product_id_seq CASCADE;
DROP SEQUENCE ext_translations_id_seq CASCADE;
DROP SEQUENCE ext_log_entries_id_seq CASCADE;
DROP TABLE ext_translations;
DROP TABLE ext_log_entries;
DROP TABLE catalog.category;
DROP TABLE catalog.product
Even with --full-database option the DROP TABLE statement is generated, but, apparently, not executed:
[vagrant#localhost thiriscart]$ ./bin/console doctrine:schema:drop --force --full-database --dump-sql
ALTER TABLE catalog.category DROP CONSTRAINT fk_d3431049727aca70;
ALTER TABLE catalog.category DROP CONSTRAINT fk_d3431049de12ab56;
ALTER TABLE catalog.category DROP CONSTRAINT fk_d343104916fe72e1;
ALTER TABLE catalog.product DROP CONSTRAINT fk_bc02688fdf6e08b4;
DROP SEQUENCE ext_log_entries_id_seq CASCADE;
DROP SEQUENCE ext_translations_id_seq CASCADE;
DROP SEQUENCE auth.user_id_seq CASCADE;
DROP SEQUENCE catalog.category_id_seq CASCADE;
DROP SEQUENCE catalog.product_id_seq CASCADE;
DROP TABLE ext_translations;
DROP TABLE ext_log_entries;
DROP TABLE auth."user";
DROP TABLE catalog.category;
DROP TABLE catalog.product
[vagrant#localhost thiriscart]$ ./bin/console doctrine:schema:update --force --dump-sql
CREATE TABLE auth."user" (id INT NOT NULL, username VARCHAR(255) NOT NULL, PRIMARY KEY(id));
ALTER TABLE catalog.category DROP CONSTRAINT FK_D3431049DE12AB56;
ALTER TABLE catalog.category DROP CONSTRAINT FK_D343104916FE72E1;
ALTER TABLE catalog.category ADD CONSTRAINT FK_D3431049DE12AB56 FOREIGN KEY (created_by) REFERENCES auth."user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE catalog.category ADD CONSTRAINT FK_D343104916FE72E1 FOREIGN KEY (updated_by) REFERENCES auth."user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE;
Updating database schema...
[Doctrine\DBAL\Exception\TableExistsException]
An exception occurred while executing 'CREATE TABLE auth."user" (id INT NOT NULL, username VARCHAR(255) NOT NULL, PRIM
ARY KEY(id))':
SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "user" already exists
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "user" already exists
[PDOException]
SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "user" already exists
doctrine:schema:update [--complete] [--dump-sql] [-f|--force] [--em [EM]]
Use doctrine:schema:update with the --complete parameter to fully sync the database with your entities.
app/console doctrine:schema:update --force --complete
or
app/console doctrine:schema:update --dump-sql --complete
Related
I created two tables in a database on MariaDB: CasaProduzione and Produzione.
create table CasaProduzione(nome varchar(80) primary key);
alter table CasaProduzione add column id tinyint;
alter table CasaProduzione drop primary key;
alter table CasaProduzione modify nome varchar(80) not null;
alter table CasaProduzione modify id tinyint primary key auto_increment;
create table Produzione(
id_film smallint not null,
id_casaProduzione tinyint not null,
data date not null,
constraint `fk_produzione`
foreign key (id_film) references Film(id),
foreign key (id_casaProduzione) references CasaProduzione(id)
on update cascade
on delete restrict);
alter table Produzione modify data in smallint(4);
After i moved the column id in the table of CasaProduzione at first
alter table CasaProduzione modify column id tinyint(4) first;
Then i tried to set auto_increment in prevoius column
alter table Produzione modify column id tinyint(4) auto_increment;
ERROR 1833 (HY000): Cannot change column 'id': used in a foreign key constraint 'Produzione_ibfk_1' of table 'Film.Produzione'
So i tried to cancel the foreign key from Produzione
alter table Produzione drop foreign key fk_produzione;
but the result is the same.
What am I doing wrong?
After suggestion from the comment, I post here the result of this command:
SHOW CREATE TABLE Film.Produzione \G;
***************************
1. row
***************************
Table: Produzione
Create Table:
CREATE TABLE Produzione (
id_film SMALLINT(6) NOT NULL,
id_casaProduzione TINYINT(4) NOT NULL,
DATA SMALLINT(4) DEFAULT NULL,
KEY fk_produzione (id_film),
KEY id_casaProduzione (id_casaProduzione),
CONSTRAINT Produzione_ibfk_1 FOREIGN KEY (id_casaProduzione) REFERENCES CasaProduzione (id) ON UPDATE CASCADE )
ENGINE=INNODB DEFAULT CHARSET=utf8mb4
1 row in set (0.001 sec)
As told by #FanoFN, with the command
show CREATE TABLE Film.Produzione \G;
the system showed me the constraint on the table Produzione. The system created the constraint Produzione_ibfk_1
I deleted this constraint with the command
alter table Produzione drop foreign key Produzione_ibfk_1;
Query OK, 0 rows affected (0.130 sec)
Records: 0 Duplicates: 0 Warnings: 0
After I can applied the command
alter table Produzione modify column id tinyint(4) auto_increment;
Thanks a lot
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.
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.
I am starting to explore Symfony and after create the schema by importing this sql script and create the entities following these commands:
Importing mapping information by generating the metadata files:
$ php app/console doctrine:mapping:import --force AcmeBlogBundle xml
Build related entity classes:
-> Generates entity classes with annotation mappings
$ php app/console doctrine:mapping:convert annotation ./src
$ php app/console doctrine:generate:entities AcmeBlogBundle
The command:
$ php app/console doctrine:schema:update --force
fails with the following error:
[Doctrine\DBAL\Exception\DriverException]
An exception occurred while executing 'ALTER TABLE writtings CHANGE id id INT AUTO_INCREMENT NOT NULL':
SQLSTATE[HY000]: General error: 1025 Error on rename of './amimusa/#sql-425_5e' to './amimusa/writtings' (errno: 150)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './amimusa/#sql-425_5e' to './amimusa/writtings' (errno: 150)
[PDOException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './amimusa/#sql-425_5e' to './amimusa/writtings' (errno: 150)
The dump file generated is:
ALTER TABLE writtings CHANGE id id INT AUTO_INCREMENT NOT NULL;
ALTER TABLE writtings ADD CONSTRAINT FK_F9A6AFF48726D6E4 FOREIGN KEY (publication_type) REFERENCES publications_type (id);
ALTER TABLE musas CHANGE id id INT AUTO_INCREMENT NOT NULL;
ALTER TABLE publications DROP FOREIGN KEY contributor_FK;
ALTER TABLE publications DROP FOREIGN KEY state_FK;
ALTER TABLE publications DROP FOREIGN KEY writting_FK;
ALTER TABLE publications CHANGE id id INT AUTO_INCREMENT NOT NULL, CHANGE id_contributor id_contributor INT DEFAULT NULL, CHANGE id_state id_state INT DEFAULT NULL, CHANGE id_writting id_writting INT DEFAULT NULL;
ALTER TABLE publications ADD CONSTRAINT FK_32783AF440C1075C FOREIGN KEY (id_writting) REFERENCES writtings (id);
ALTER TABLE publications ADD CONSTRAINT FK_32783AF44D1693CB FOREIGN KEY (id_state) REFERENCES states (id);
ALTER TABLE publications ADD CONSTRAINT FK_32783AF4C27D5A64 FOREIGN KEY (id_contributor) REFERENCES contributors (id);
ALTER TABLE publications_musas DROP FOREIGN KEY musa_FK;
ALTER TABLE publications_musas DROP FOREIGN KEY publication_FK;
ALTER TABLE publications_musas DROP FOREIGN KEY musa_FK;
ALTER TABLE publications_musas CHANGE id_publication id_publication INT NOT NULL, CHANGE id_musa id_musa INT NOT NULL;
ALTER TABLE publications_musas ADD CONSTRAINT FK_7EF2161FB72EAA8E FOREIGN KEY (id_publication) REFERENCES publications (id);
ALTER TABLE publications_musas ADD CONSTRAINT FK_7EF2161FFB53D80 FOREIGN KEY (id_musa) REFERENCES musas (id);
DROP INDEX musa_fk ON publications_musas;
CREATE INDEX IDX_7EF2161FFB53D80 ON publications_musas (id_musa);
ALTER TABLE publications_musas ADD CONSTRAINT musa_FK FOREIGN KEY (id_musa) REFERENCES musas (id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE contributors CHANGE id id INT AUTO_INCREMENT NOT NULL;
ALTER TABLE states CHANGE id id INT AUTO_INCREMENT NOT NULL;
Anybody could help me to understand why the update fails?
Also I can't figure out why these operations are required since everything has been created from scratch.
As Cerad commented:
The reverse engineering stuff is not perfect. Doctrine likes to have
things defined it's own way. I'd suggest dropping the database then
use doctrine:schema:create to rebuild it the way doctrine wants it to
be.
$ php app/console doctrine:schema:drop --force
$ php app/console doctrine:schema:create
Has fixed the issue.
I have the following 5 tables defined with a few records inserted into the 1st 4. This is using sqlite 3.7.1.7 with foreign key constaint enabled.
create table if not exists subject (id varchar(50) primary key,desc varchar(100));
insert into subject (id,desc) values ("subject1","test subject");
create table if not exists subjectlevel (id_subject_id varchar(50) references subject(id) on delete cascade, id integer not null, desc varchar(100) not null, questmcmaxselections integer not null, primary key (id_subject_id,id));
insert into subjectlevel (id_subject_id,id,desc,questmcmaxselections) values ("subject1",1,"test subject1 level 1",4);
insert into subjectlevel (id_subject_id,id,desc,questmcmaxselections) values ("subject1",2,"test subject1 level 2",4);
create table if not exists questmc (id integer primary key, text varchar(300) not null, includeallanswers int not null, subject_id varchar(50), subjectlevel_id integer, foreign key (subject_id, subjectlevel_id) references subjectlevel (id_subject_id,id) on delete cascade);
insert into questmc (text,includeallanswers,subject_id,subjectlevel_id) values ("this is a _ question", 1, "subject1",1);
create table if not exists questmcselection (id integer primary key, text varchar(100) not null, subject_id varchar(50), subjectlevel_id integer, foreign key (subject_id, subjectlevel_id) references subjectlevel (id_subject_id,id) on delete cascade);
insert into questmcselection (text,subject_id,subjectlevel_id) values ("this is a solution","subject1",1);
create table if not exists questmc_questmcselection(id integer primary key, answer integer not null, questmc_id integer, questmcselection_id integer, subject_id varchar(50), subjectlevel_id integer, foreign key (questmc_id) references questmc(id) on delete cascade, foreign key (questmcselection_id) references questmcselection (id) on delete cascade, foreign key (subject_id,subjectlevel_id) references questmc (subject_id,subjectlevel_id) on delete cascade, foreign key (subject_id,subjectlevel_id) references questmcselection (subject_id,subjectlevel_id));
if i attempt to delete the second record in the subjectlevel table, i get a foreign key mismatch error as long as table questmc_questmcselection is defined.
sqlite> delete from subjectlevel where id=2;
Error: foreign key mismatch - "questmc_questmcselection" referencing "questmcselection"
questmc, questmcselection, and questmc_questmcselection have no related existing records that should prevent this deletion. Any idea why this error occurs?
This error has nothing to do with this particular subjectlevel record.
Your problem is that your tables lack the required indexes.
This was not reported earlier because that DELETE statement was the first command that required SQLite to check the consistency of the database schema.
Based on CL's answer -
sqlite> create table parent(a);
sqlite> create table child(a, FOREIGN KEY (a) REFERENCES parent(a));
sqlite> pragma foreign_keys = ON;
sqlite> insert into parent values(3);
sqlite> insert into child values (3);
Error: foreign key mismatch - "child" referencing "parent"
sqlite> create unique index p_a on parent(a);
sqlite> insert into child values (3);
sqlite> _
From the documentation:
Usually, the parent key of a foreign key constraint is the primary key
of the parent table. If [not], then the parent key columns must be
collectively subject to a UNIQUE constraint or have a UNIQUE index [which uses]
the collation sequences ... in the CREATE TABLE
statement for the parent table.
i.e. the alternative is:
sqlite> create table parent(a, b, UNIQUE (a, b));
sqlite> create table child (x, y, FOREIGN KEY (x, y) REFERENCES parent(a, b));
(this also highlights multi-column foreign keys; they work with indexes too...)