Doctrine创建表,但不删除它

时间:2015-11-08 18:22:08

标签: symfony doctrine-orm

当我为我的实体创建表时,Doctrine创建auth."user"表没问题:

[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

但不要放弃它:

[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

即使使用--full-database选项,也会生成DROP TABLE语句,但显然未执行:

[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]]

1 个答案:

答案 0 :(得分:2)

使用doctrine:schema:update--complete参数将数据库与您的实体完全同步。

app/console doctrine:schema:update --force --complete

app/console doctrine:schema:update --dump-sql --complete