Sep-26-2018, 09:48 AM
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.
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!
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!