[Answered ]-Django how to create model tables in a specific schema

1πŸ‘

βœ…

As stated in link below, you need to specify schema name and table name in db_table, so your Meta class for User should look like this (with default schema):

class Meta: 
    db_table = 'public"."user'

And for Car (assuming schema called "new"):

class Meta: 
    db_table = 'new"."car'

Don’t forget to migrate this.

How to create tables in a different schema in django?

πŸ‘€COSHW

0πŸ‘

I am assuming your suggesting something like this:

class User(models.Model):
    name = models.CharField(max_length=200)
    age = models.PositiveBigIntegerField()
    class Meta: 
        db_table = 'user'

class Car(models.Model):
    name = models.CharField(max_length=200)
    model = models.PositiveBigIntegerField()
    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    class Meta: 
        db_table = 'car'

Here we are using a owner in Car model to create a relationship to User model.

πŸ‘€eagele

Leave a comment