Issue whe migrate with relation between tables in different schemas

54 views
Skip to first unread message

felipe.per...@gmail.com

unread,
Jan 6, 2017, 1:32:55 PM1/6/17
to Django users
I'm working with Django 1.9.7 with Postgresql and I stuck in a problem with relations between tables in different schemas.

The problem that I'm facing is that I have a table, for example, named product, which is in a schema named dataflex and another table named client_product_sale_stc in another schema.
When I try to migrate a relation between client_product_sale_stc and product, I got this error:

django.db.utils.ProgrammingError: ERRO:  syntax error at or near "dataflex"
LINE 1: ...CONSTRAINT "client_product_setor_id_52aad07a_fk_"dataflex"....

After sometime trying to figure It out a solution for this problem, I tried to change the schemas's name to just df instead of dataflex and It worked perfectly.

I also tried to decreased table's name, and It worked just like when I decreased schema's name size, so this It seems to be a problem with big constraints names.


I'm using the follow syntax:

db_table = '"dataflex"."product"'


Is this same problem with djando and postgresql big names on relation between diffent schemas?

Is there any other way to fix It? 


OBS:setor_id is the name of the field of the relation in client_product_sale_stx model/table. 

product model/table does not have a field for the relation.




Melvyn Sopacua

unread,
Jan 14, 2017, 12:20:54 PM1/14/17
to django...@googlegroups.com

Hi,

 

On Friday 06 January 2017 10:29:34 felipe.per...@gmail.com wrote:

> I'm working with Django 1.9.7 with Postgresql and I stuck in a problem

> with relations between tables in different schemas.

>

> The problem that I'm facing is that I have a table, for example, named

> *product, *which is in a schema named *dataflex *and another table

> named *client_product_sale_stc *in another schema.

> When I try to migrate a relation between *client_product_sale_stc *and

> *product, *I got this error:

>

> django.db.utils.ProgrammingError: ERRO: syntax error at or near

> "dataflex" LINE 1: ...CONSTRAINT

> "client_product_setor_id_52aad07a_fk_"dataflex"....

>

>

> After sometime trying to figure It out a solution for this problem, I

> tried to change the schemas's name to just df instead of dataflex and

> It worked perfectly.

>

> I also tried to decreased table's name, and It worked just like when I

> decreased schema's name size, so this It seems to be a problem with

> big constraints names.

>

>

> I'm using the follow syntax:

>

> *db_table = '"dataflex"."product"'*

 

Django has no support for database schema's and a bit of duckducking would've shown you that. For example this ticket shows that it may or may not be coming in Django 2.0, but almost certainly not in a 1.x release.

So, any errors caused by you trying to work around it, are on you and when the implementation does land in 2.0, you'll be stuck with your work arounds. Think hard about that before trying to go that route.

 

Now, there's one app I know of that does use the PostgreSQL schema support, but implemented at the project level, not on a table level and for a specific purpose.

 

And finally, there's this approach, where you'd have to write the routing yourself - trivial to do and when done correctly should be easy to refactor to any solution Django comes up with in future releases.

 

In no way, should you mess with official Django model properties to trick the ORM - as you are now seeing that goes down the wrong path.

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages