Hello, Everyone
I am working with Django + MS SQL Server
I need to create a primary key manually. I am new to framework. I tried django-sequences but not working with SQL Server. I am facing issues where should I increment my primary key in models or in view.py. I tried to create primary key manually in views.py ‘perform_create’ method I am saving serializer.save() in perform_create method also in my create method. Anyone can give me a complete code snippet how I can generate PK manually. I’ll put code below.
Anyone can please help. thanks
In database ‘UniqID’ is not AutoIncremental field. If I set UniqID is Integer field in my database I get error. Either I put manually PK everytime or whenever data is inserted it auto increment the UiqID
I attached the SC.
class CustomerSerializer(serializers.ModelSerializer):
uniqid = serializers.ReadOnlyField(read_only=True)
class Meta:
model = DrycleanMcustomerinfomaster
fields = ['uniqid', 'customer_first_name', 'customer_phone', 'customer_email_address','customer_terms_conditions','frenchise']
def validate(self, data):
customer_first_name = data.get('customer_first_name', '')
customer_phone = data.get('customer_phone', '') # get - Provide defualt param is email not inserted
customer_email_address = data.get('customer_email_address', '')
customer_terms_conditions = data.get('customer_terms_conditions', '')
frenchise = data.get('frenchise', '') # get - Provide defualt param is email not inserted
if customer_first_name == "":
raise serializers.ValidationError("name is missing")
return data
No, it just currently looks like it’s working fine. But you have a race condition in your code that will periodically throw errors.
You fix and prevent this by creating your primary key properly - as an auto increment field. (Or, you create the appropriate database-level code - trigger or UDF - to create that field. What you don’t do is try to manage it within Django.)
I’m not sure I understand what you’re asking here.
You would not be “accessing” or “using” those triggers from within your Django code - they would work on the data being submitted to the database as sent from Django. You would effectively be recreating the “autoincrement” functionality within your own code instead of using the feature as provided by the database.