Django Models – Django admin is throwing a DataError for "setting" a string to an integer within a Foreign Key
Question:
I’ve setup some models for Stripe payments, one for Product and the other for Price. You can see I’ve set the API value from Stripe as the Primary Key.
These are their fields:
class Product(models.Model):
stripe_product_id = models.CharField(max_length=100, primary_key=True)
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Price(models.Model):
stripe_price_id = models.CharField(max_length=100, primary_key=True)
product = models.ForeignKey(
Product, on_delete=models.CASCADE, to_field="stripe_product_id")
price = models.IntegerField(default=0)
Django admin let me create Product fine using the string api key, then when I went to create a Price object using the Price API value from Stripe, and having the foreign key set to my Product. I was given the following error (my api values are redacted):
DataError: invalid input syntax for integer: "prod_xyz" LINE 1: UPDATE "payments_price" SET "product_id" = 'prod_xyz...
I tried using the to_field
parameter in Foreign Key but that didn’t seem to fix it.
Does anyone know the method to fix it?
Or should I just go back to using the default primary key with the product/price id as Unique?
Answers:
class Product(models.Model):
stripe_product_id = models.IntegerField(max_length=100, primary_key=True)
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Price(models.Model):
stripe_price_id = models.IntegerField(default=0)
product = models.ForeignKey(
Product, on_delete=models.CASCADE, to_field="stripe_product_id")
price = models.IntegerField(default=0)
Wasn’t able to find an answer to why Django Foreign Key tries to call the non-existant default primary key. So I just utilised the Unique keyword instead of setting a custom primary key.
I’ve setup some models for Stripe payments, one for Product and the other for Price. You can see I’ve set the API value from Stripe as the Primary Key.
These are their fields:
class Product(models.Model):
stripe_product_id = models.CharField(max_length=100, primary_key=True)
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Price(models.Model):
stripe_price_id = models.CharField(max_length=100, primary_key=True)
product = models.ForeignKey(
Product, on_delete=models.CASCADE, to_field="stripe_product_id")
price = models.IntegerField(default=0)
Django admin let me create Product fine using the string api key, then when I went to create a Price object using the Price API value from Stripe, and having the foreign key set to my Product. I was given the following error (my api values are redacted):
DataError: invalid input syntax for integer: "prod_xyz" LINE 1: UPDATE "payments_price" SET "product_id" = 'prod_xyz...
I tried using the to_field
parameter in Foreign Key but that didn’t seem to fix it.
Does anyone know the method to fix it?
Or should I just go back to using the default primary key with the product/price id as Unique?
class Product(models.Model):
stripe_product_id = models.IntegerField(max_length=100, primary_key=True)
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Price(models.Model):
stripe_price_id = models.IntegerField(default=0)
product = models.ForeignKey(
Product, on_delete=models.CASCADE, to_field="stripe_product_id")
price = models.IntegerField(default=0)
Wasn’t able to find an answer to why Django Foreign Key tries to call the non-existant default primary key. So I just utilised the Unique keyword instead of setting a custom primary key.