Python Forum
Python 3.6, MySQL 8.0.12, SQLAlchemy 1.2.10, reflection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.6, MySQL 8.0.12, SQLAlchemy 1.2.10, reflection
#1
Recently, I installed SQLAlchemy 1.2.10 to start learning SQLAlchemy, using Python 3.6.6 and MySQL 8.0.12. Just as SQL Server has a built in DB (Northwind/AdventureWorks/Worldwide Importers), I have downloaded a test DB for MySQL, Chinook (Chinook_MySql_AutoIncrementPKs.sql) from https://github.com/lerocha/chinook-datab...ataSources.
I ran the SQL file and MySQL DB created the Chinook DB successfully.

As part of learning reflection, when I run the following script, I see errors.
#!/usr/bin/python3.6

from sqlalchemy import MetaData, create_engine, Table
from sqlalchemy import select

engine = create_engine('mysql+pymysql://laxman:********@localhost/Chinook', pool_recycle=3600)
metadata = MetaData(engine)

artist = Table('Artist', metadata, autoload=True, autoload_with=engine)
print(artist.columns.keys())
s = select([artist]).limit(10) 
result = engine.execute(s).fetchall()
for row in result:
	print(row)

album = Table('Album', metadata, autoload=True, autoload_with=engine)
print(str(artist.join(album)))
print(metadata.tables[album])
Error:
Traceback (most recent call last): File "sqlalchemy_reflect_tables.py", line 17, in <module> print(str(artist.join(album))) File "/home/laxman/.local/lib/python3.6/site-packages/sqlalchemy/sql/selectable.py", line 451, in join return Join(self, right, onclause, isouter, full) File "/home/laxman/.local/lib/python3.6/site-packages/sqlalchemy/sql/selectable.py", line 797, in __init__ self.onclause = self._match_primaries(self.left, self.right) File "/home/laxman/.local/lib/python3.6/site-packages/sqlalchemy/sql/selectable.py", line 931, in _match_primaries return self._join_condition(left, right, a_subset=left_right) File "/home/laxman/.local/lib/python3.6/site-packages/sqlalchemy/sql/selectable.py", line 964, in _join_condition a, a_subset, b, consider_as_foreign_keys) File "/home/laxman/.local/lib/python3.6/site-packages/sqlalchemy/sql/selectable.py", line 1002, in _joincond_scan_left_right col = fk.get_referent(left) File "/home/laxman/.local/lib/python3.6/site-packages/sqlalchemy/sql/schema.py", line 1752, in get_referent return table.corresponding_column(self.column) File "/home/laxman/.local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 767, in __get__ obj.__dict__[self.__name__] = result = self.fget(obj) File "/home/laxman/.local/lib/python3.6/site-packages/sqlalchemy/sql/schema.py", line 1903, in column tablekey, colname) sqlalchemy.exc.NoReferencedColumnError: Could not initialize target column for ForeignKey 'Artist.artistid' on table 'Album': table 'Artist' has no column named 'artistid'
The above error is generated when executing the line:
print(str(artist.join(album)))
The join condition fails between tables 'Artist' and 'Album'. From the SQL script that creates Chinook DB, I see a foreign key constraint named "FK_AlbumArtistId"

ALTER TABLE Album ADD CONSTRAINT FK_AlbumArtistId FOREIGN KEY (ArtistId) REFERENCES Artist (ArtistId) ON DELETE NO ACTION ON UPDATE NO ACTION;
CREATE INDEX IFK_AlbumArtistId ON Album (ArtistId);

If I wish to include the SQL script that creates Chinook db, how do I attach upload the file?
I looked at the SQL script that created the Chinook DB, the installed DB in MySQL, my Python code. I see reference to foreign key 'Artist.ArtistId' but find no reference to foreign key 'Artist.artistid'. So, from where does Python pick up column name 'artistid'? What am I missing? How do I resolve this issue?

My environment:
OS: Ubuntu Linux 18.04 (64 bit)
Python: 3.6.6
MySQL: 8.0.12
SQLAlchemy: 1.2.10

Any insights/suggestions welcome. Thanks in advance!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 664 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,991 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,668 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Python and MySql ogautier 8 3,336 May-20-2021, 11:10 PM
Last Post: Pedroski55
  Python and MySQL Pedroski55 5 3,216 Mar-23-2021, 06:40 AM
Last Post: ndc85430
  Python MySQL ogautier 0 2,101 Sep-03-2020, 03:54 PM
Last Post: ogautier
  Insert into mysql through python LaKhWaN 0 1,940 Aug-26-2020, 04:54 AM
Last Post: LaKhWaN
  Python mysql query help please tduckman 4 4,308 Mar-13-2020, 03:42 PM
Last Post: Marbelous
  mysql db connection using python sunstar20 13 11,977 Aug-04-2018, 09:08 AM
Last Post: vishalhule
  A combination of Python and MySql xgrzeniu 2 3,970 Mar-28-2018, 06:50 AM
Last Post: xgrzeniu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020