1

So this is the question: Let's say I have 2 schemas in my database: schema1 and schema2

I have a simple user table and a simple car table

class User(models.Model)
    class Meta:
        db_table = 'user'


class Car(models.Model)
    class Meta:
        db_table = 'car'

I want the user table to be in schema1

And the car table is to be in schema2

How would you guys go about doing this?

I saw a post that said I need to specify int he db_table = 'schema_name"."table_name' But that doesn't work.

Matan
  • 73
  • 2
  • 14

2 Answers2

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
  • 146
  • 8
  • 1
    This is so weird. I saw that post and even specified in my question that this way doesn't work for me for some reason. But I rerun the code on a different PC and it worked. maybe the django version or something I did with the migrations. either way it worked so thanks! – Matan Mar 25 '23 at 07:59
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
  • 66
  • 2
  • Thanks for trying to answer, but nowhere in my question I was talking about relationships between tables. The question was how can I create each table in a different schema. – Matan Mar 25 '23 at 07:52