lolopinto / ent

MIT License
51 stars 6 forks source link

constraint names are unique across the db #252

Open lolopinto opened 3 years ago

lolopinto commented 3 years ago

add checks for that in the schema

db:

ent-rsvp=# \d+ guests;
                                                 Table "public.guests"
     Column     |            Type             | Collation | Nullable | Default | Storage  | Stats target | Description 
----------------+-----------------------------+-----------+----------+---------+----------+--------------+-------------
 id             | uuid                        |           | not null |         | plain    |              | 
 created_at     | timestamp without time zone |           | not null |         | plain    |              | 
 updated_at     | timestamp without time zone |           | not null |         | plain    |              | 
 first_name     | text                        |           | not null |         | extended |              | 
 last_name      | text                        |           | not null |         | extended |              | 
 email_address  | text                        |           | not null |         | extended |              | 
 event_id       | uuid                        |           | not null |         | plain    |              | 
 guest_group_id | uuid                        |           | not null |         | plain    |              | 
Indexes:
    "guests_id_pkey" PRIMARY KEY, btree (id)
    "uniqueEmail" UNIQUE CONSTRAINT, btree (event_id, email_address)
Foreign-key constraints:
    "guests_event_id_fkey" FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
    "guests_guest_group_id_fkey" FOREIGN KEY (guest_group_id) REFERENCES guest_groups(id) ON DELETE CASCADE
Referenced by:
    TABLE "auth_codes" CONSTRAINT "auth_codes_guest_id_fkey" FOREIGN KEY (guest_id) REFERENCES guests(id) ON DELETE CASCADE
    TABLE "guest_data" CONSTRAINT "guest_data_guest_id_fkey" FOREIGN KEY (guest_id) REFERENCES guests(id) ON DELETE CASCADE

ent-rsvp=# \d+ guest_groups;
                                              Table "public.guest_groups"
     Column      |            Type             | Collation | Nullable | Default | Storage  | Stats target | Description 
-----------------+-----------------------------+-----------+----------+---------+----------+--------------+-------------
 id              | uuid                        |           | not null |         | plain    |              | 
 created_at      | timestamp without time zone |           | not null |         | plain    |              | 
 updated_at      | timestamp without time zone |           | not null |         | plain    |              | 
 invitation_name | text                        |           | not null |         | extended |              | 
 event_id        | uuid                        |           | not null |         | plain    |              | 
Indexes:
    "guest_groups_id_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "guest_groups_event_id_fkey" FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
Referenced by:
    TABLE "guests" CONSTRAINT "guests_guest_group_id_fkey" FOREIGN KEY (guest_group_id) REFERENCES guest_groups(id) ON DELETE CASCADE

ent-rsvp=# 

error:

ola@OlaspersonalMBP backend % tsent codegen
2021/03/19 13:45:09 wrote to file  /Users/ola/code/ent/examples/ent-rsvp/backend/src/schema/schema.py
Generating /Users/ola/code/ent/examples/ent-rsvp/backend/src/schema/versions/89609129b588_2021319204510_add_column_email_address_to_table_guest_.py ...  done
Running post write hook "autopep8" ...
  done
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1276, in _execute_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.DuplicateTable: relation "uniqueEmail" already exists

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/auto_schema", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/site-packages/auto_schema/cli/__init__.py", line 41, in main
    r.run()
  File "/usr/local/lib/python3.8/site-packages/auto_schema/runner.py", line 208, in run
    self._apply_changes(diff)
  File "/usr/local/lib/python3.8/site-packages/auto_schema/runner.py", line 219, in _apply_changes
    self.upgrade()
  File "/usr/local/lib/python3.8/site-packages/auto_schema/runner.py", line 302, in upgrade
    self.cmd.upgrade()
  File "/usr/local/lib/python3.8/site-packages/auto_schema/command.py", line 54, in upgrade
    command.upgrade(self.alembic_cfg, revision)
  File "/usr/local/lib/python3.8/site-packages/alembic/command.py", line 298, in upgrade
    script.run_env()
  File "/usr/local/lib/python3.8/site-packages/alembic/script/base.py", line 489, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/usr/local/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/lib/python3.8/site-packages/alembic/util/compat.py", line 184, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/lib/python3.8/site-packages/auto_schema/env.py", line 133, in <module>
    run_migrations_online()
  File "/usr/local/lib/python3.8/site-packages/auto_schema/env.py", line 127, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/usr/local/lib/python3.8/site-packages/alembic/runtime/environment.py", line 846, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/usr/local/lib/python3.8/site-packages/alembic/runtime/migration.py", line 522, in run_migrations
    step.migration_fn(**kw)
  File "/Users/ola/code/ent/examples/ent-rsvp/backend/src/schema/versions/89609129b588_2021319204510_add_column_email_address_to_table_guest_.py", line 27, in upgrade
    op.create_unique_constraint('uniqueEmail', 'guest_groups', [
  File "<string>", line 8, in create_unique_constraint
  File "<string>", line 3, in create_unique_constraint
  File "/usr/local/lib/python3.8/site-packages/alembic/operations/ops.py", line 439, in create_unique_constraint
    return operations.invoke(op)
  File "/usr/local/lib/python3.8/site-packages/alembic/operations/base.py", line 373, in invoke
    return fn(self, operation)
  File "/usr/local/lib/python3.8/site-packages/alembic/operations/toimpl.py", line 151, in create_constraint
    operations.impl.add_constraint(
  File "/usr/local/lib/python3.8/site-packages/alembic/ddl/impl.py", line 245, in add_constraint
    self._exec(schema.AddConstraint(const))
  File "/usr/local/lib/python3.8/site-packages/alembic/ddl/impl.py", line 141, in _exec
    return conn.execute(construct, *multiparams, **params)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1011, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/ddl.py", line 72, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1068, in _execute_ddl
    ret = self._execute_context(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1316, in _execute_context
    self._handle_dbapi_exception(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1510, in _handle_dbapi_exception
    util.raise_(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1276, in _execute_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateTable) relation "uniqueEmail" already exists

[SQL: ALTER TABLE guest_groups ADD CONSTRAINT "uniqueEmail" UNIQUE (event_id, email_address)]
(Background on this error at: http://sqlalche.me/e/13/f405)
2021/03/19 13:45:11 cmd.Run() failed with exit status 1
lolopinto commented 3 years ago

also https://github.com/lolopinto/ent/issues/83 relevant here since the state is sadness