Repository: fossasia/open-event-server

Issue:
Cannot create ticket with name "Business Ticket" if former ticket with name "Business Ticket" was deleted
Cannot create ticket with name "Business Ticket" if former ticket with name "Business Ticket" was deleted. Error message appears.


Discussion:
@iamareebjamal Isnt it duplicate of https://github.com/fossasia/open-event-server/issues/5871 ?
No
```
(psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "name_event_uc"
DETAIL:  Key (name, event_id)=(Business Ticket, 1170) already exists.

[SQL: UPDATE tickets SET name=%(name)s WHERE tickets.id = %(tickets_id)s]
[parameters: {'name': 'Business Ticket', 'tickets_id': 1265}]
```

Append `-deleted` to the ticket name when deleting. Similar to what we did in user email field
@iamareebjamal Shouldn't the ticket be deleted when we click remove ?
Why the id is still stored ?, Cant we do hard delete for tickets ?
@kushthedude I think there should be a log for everything in the database, so soft deletion method followed for deletion of tickets.
Tickets may be related to orders, so first of all, any ticket with placed order should not be able to be deleted, but this is another issue. Secondly, people make mistakes, and regarding events, tickets, and orders are the most important things related to an event in an event management system, so no, they shouldn't be hard deleted.
@iamareebjamal For this issue the name of ticket shall be changed in`beforedeleteobject` method ? and also the permission has to be checked or not?


If it can be done in the same PR reliably, then great
@iamareebjamal I am stuck on beforedeleteibject method, I can't figure how to update the schema before the deletion, do i have to perform a db session transaction here or you can give me a example where it's done.

Or shall the saveToDb can be done after the ticket is deleted? which one will be appropriate?
` I can't figure how to update the schema before the deletion`

Why do you want to do that?
> `I can't figure how to update the schema before the deletion`
> 
> Why do you want to do that?


`Append -deleted to the ticket name when deleting. Similar to what we did in user email field`

@iamareebjamal 
@codedsun Please stick to one issue at a time. 
@kushthedude At least he's sending PRs to urgent issues within a day
@codedsun It's not called updating the schema. It's updating DB, and see how it's done when deleting a user
@kushthedude - I love learning here and also this issue is not very major
one..I am working on it side by side..also as I am new to django, I will
take time to figure things out here.

Also, I am free all day and night, what else shall I do..

If you can want you can take this up. :)

On Wed, Nov 27, 2019, 10:27 PM Areeb Jamal <notifications@github.com> wrote:

> @kushthedude <https://github.com/kushthedude> At least he's sending PRs
> to urgent issues within a day
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/fossasia/open-event-server/issues/6621?email_source=notifications&email_token=AEYZISAZ2PJS2AKOYWR2WMLQV2RIVA5CNFSM4JR4XU3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFKENZY#issuecomment-559171303>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AEYZISFJUOIK5SZHA7XYKSDQV2RIVANCNFSM4JR4XU3A>
> .
>


Resolving patch:
diff --git a/app/models/ticket.py b/app/models/ticket.py
--- a/app/models/ticket.py
+++ b/app/models/ticket.py
@@ -22,7 +22,7 @@
 
 class Ticket(SoftDeletionModel):
     __tablename__ = 'tickets'
-    __table_args__ = (db.UniqueConstraint('name', 'event_id', name='name_event_uc'),)
+    __table_args__ = (db.UniqueConstraint('name', 'event_id', 'deleted_at', name='name_event_deleted_at_uc'),)
 
     id = db.Column(db.Integer, primary_key=True)
     name = db.Column(db.String, nullable=False)
diff --git a/migrations/versions/rev-2019-12-04-00:37:24-0223c881d135_.py b/migrations/versions/rev-2019-12-04-00:37:24-0223c881d135_.py
new file mode 100644
--- /dev/null
+++ b/migrations/versions/rev-2019-12-04-00:37:24-0223c881d135_.py
@@ -0,0 +1,30 @@
+"""empty message
+
+Revision ID: 0223c881d135
+Revises: d1c2b8711223
+Create Date: 2019-12-04 00:37:24.171728
+
+"""
+
+from alembic import op
+import sqlalchemy as sa
+import sqlalchemy_utils
+
+
+# revision identifiers, used by Alembic.
+revision = '0223c881d135'
+down_revision = 'd1c2b8711223'
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.create_unique_constraint('name_event_deleted_at_uc', 'tickets', ['name', 'event_id', 'deleted_at'])
+    op.drop_constraint('name_event_uc', 'tickets', type_='unique')
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.create_unique_constraint('name_event_uc', 'tickets', ['name', 'event_id'])
+    op.drop_constraint('name_event_deleted_at_uc', 'tickets', type_='unique')
+    # ### end Alembic commands ###


Test patch: