question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cross-database foreign keys do not work

See original GitHub issue

Using the ForeignKeyField inside a model with a rel_model that’s located inside another database than the aforementioned model peewee won’t resolve the related model to its correct database. As a result, tables containing foreign keys with related models of other databases cannot be created:

Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2406, in execute_sql
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/cursors.py", line 132, in execute
    result = self._query(query)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/cursors.py", line 271, in _query
    conn.query(q)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 744, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 879, in _read_query_result
    result.read()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 1082, in read
    first_packet = self.connection._read_packet()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 844, in _read_packet
    packet.check_error()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 370, in check_error
    raise_mysql_exception(self._data)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/err.py", line 116, in raise_mysql_exception
    _check_mysql_exception(errinfo)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/err.py", line 112, in _check_mysql_exception
    raise InternalError(errno, errorvalue)
pymysql.err.InternalError: (1005, "Can't create table 'realestatedb.realestate' (errno: 150)")

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./setup.py", line 22, in <module>
    table.create_table(fail_silently=True)
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 3078, in create_table
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2471, in create_table
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2414, in execute_sql
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2283, in __exit__
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 105, in reraise
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2406, in execute_sql
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/cursors.py", line 132, in execute
    result = self._query(query)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/cursors.py", line 271, in _query
    conn.query(q)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 744, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 879, in _read_query_result
    result.read()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 1082, in read
    first_packet = self.connection._read_packet()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 844, in _read_packet
    packet.check_error()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 370, in check_error
    raise_mysql_exception(self._data)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/err.py", line 116, in raise_mysql_exception
    _check_mysql_exception(errinfo)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/err.py", line 112, in _check_mysql_exception
    raise InternalError(errno, errorvalue)
peewee.InternalError: (1005, "Can't create table 'realestatedb.realestate' (errno: 150)")

And, if the databases were then manually created, it cannot assign the correct foreign key fields:

    Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2406, in execute_sql
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/cursors.py", line 132, in execute
    result = self._query(query)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/cursors.py", line 271, in _query
    conn.query(q)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 744, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 879, in _read_query_result
    result.read()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 1082, in read
    first_packet = self.connection._read_packet()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 844, in _read_packet
    packet.check_error()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 370, in check_error
    raise_mysql_exception(self._data)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/err.py", line 116, in raise_mysql_exception
    _check_mysql_exception(errinfo)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/err.py", line 112, in _check_mysql_exception
    raise InternalError(errno, errorvalue)
pymysql.err.InternalError: (1054, "Unknown column 't1.customer_id' in 'field list'")

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2139, in __iter__
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2132, in execute
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 1838, in _execute
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2414, in execute_sql
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2283, in __exit__
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 105, in reraise
  File "/usr/lib/python3.4/site-packages/peewee-2.2.4-py3.4.egg/peewee.py", line 2406, in execute_sql
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/cursors.py", line 132, in execute
    result = self._query(query)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/cursors.py", line 271, in _query
    conn.query(q)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 744, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 879, in _read_query_result
    result.read()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 1082, in read
    first_packet = self.connection._read_packet()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 844, in _read_packet
    packet.check_error()
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/connections.py", line 370, in check_error
    raise_mysql_exception(self._data)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/err.py", line 116, in raise_mysql_exception
    _check_mysql_exception(errinfo)
  File "/usr/lib/python3.4/site-packages/PyMySQL-0.6.2-py3.4.egg/pymysql/err.py", line 112, in _check_mysql_exception
    raise InternalError(errno, errorvalue)
peewee.InternalError: (1054, "Unknown column 't1.customer_id' in 'field list'")

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Oct 24, 2014

You might try using the schema model option. This is typically reserved for referencing tables that belong to a schema, but it might work for your use-case as well.

class CRMModel(Model):
    """
    Generic HOMEINFO-DB Model
    """
    class Meta:
        database = database
        schema = 'homeinfodb'

# ...


class REDBModel(Model):
    class Meta:
        database = database
        schema = 'realestatedb'
0reactions
coleifercommented, Oct 30, 2014

Please re-open if using schema does not resolve the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Foreign Key relationship between two Databases
The short answer is that SQL Server (as of SQL 2008) does not support cross database foreign keys--as the error message states.
Read more >
How do I get around 'cross-database foreign key references ...
Yes dear,. This is not good idea, as its use trigger or replicate table. The reason is to you just to search things...
Read more >
Cross-database foreign keys vs. database design best practice
And cross-schema foreign keys are perfectly fine, but you can't do cross-database fkeys.
Read more >
Cross-database foreign key references are not supported ...
When I try to create a cross-database foreign key constraint, I get "Cross-database foreign key references are not supported" The cause of the...
Read more >
3 common foreign key mistakes (and how to avoid them)
The biggest foreign key mistake of all is not using foreign keys, and instead relying on informal links between databases. A quick example...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found