# Problems with foreign key

**URL:** <https://forum.djangoproject.com/t/problems-with-foreign-key/24965>\
**Category:** Getting Started\
**Created:** [November 1, 2023, 11:03pm UTC](https://forum.djangoproject.com/t/problems-with-foreign-key/24965 "2023-11-01T23:03:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chiino147](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chiino147/32/16773_2.png) [@Chiino147](https://forum.djangoproject.com/u/Chiino147)\
**Post date:** [November 1, 2023, 11:03pm UTC](https://forum.djangoproject.com/t/problems-with-foreign-key/24965/1 "2023-11-01T23:03:16Z")

</div>

I’m learning to use models with relationships to other tables

```auto
class Stock(models.Model): #Modelo del stock
    cod_art = models.IntegerField(primary_key=True)
    descripcion = models.CharField(max_length=50)
    stock = models.IntegerField()
    class Meta:
        managed = False
        db_table = 'stock'

class Pedido(models.Model): #Modelo para los pedidos
    cod_pedido = models.AutoField(primary_key=True)
    #cod_detalle = models.IntegerField()
    cod_detalle = models.ForeignKey(Stock, on_delete=models.CASCADE)
    user= models.CharField(max_length=50)
    class Meta:
        managed = False
        db_table = 'pedido'

```

I have these 2 tables: “Orders” and “Stock”. My problem is when creating an order, I want to store the product ID and when performing the .save() I get the following error  
**(1054, “Unknown column ‘cod\_detalle\_id’ in ‘field list’”)**

The code he made to save it in the database is this:

```auto
    s = Stock(cod_art = 8 , descripcion = "lapicera verde", stock = 10)
    
    s.save()
    
    p = Pedido.objects.create( cod_detalle = s , user = 44 )

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 1, 2023, 11:44pm UTC](https://forum.djangoproject.com/t/problems-with-foreign-key/24965/2 "2023-11-01T23:44:36Z")

</div>

Side note: When requesting assistance with an error message, please post the complete traceback of the message, along with the complete function being executed where the error occurs.

In this case, also please include any save methods that you may have implemented in the model or form.

---

<div class="post-metadata">

**Author:** ![Chiino147](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chiino147/32/16773_2.png) [@Chiino147](https://forum.djangoproject.com/u/Chiino147)\
**Post date:** [November 2, 2023, 12:03am UTC](https://forum.djangoproject.com/t/problems-with-foreign-key/24965/3 "2023-11-02T00:03:45Z")

</div>

Sorry for not putting all the information!  
I kept looking for information and managed to solve it by adding the “db\_column” argument

```auto
cod_detalle = models.ForeignKey(Stock, on_delete=models.CASCADE, db_column='cod_detalle')

```
