Cross-database foreign keys do not work
See original GitHub issueUsing 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:
- Created 9 years ago
- Comments:6 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.Please re-open if using
schema
does not resolve the issue.