Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLalchemy not find table for creating foreign key

I have a problem with SQL Alchemy, while trying to create a database, i get:

"sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'estate_agent.person_id' could not find table 'person' with which to generate a foreign key to target column 'id'" 

Meta datas:

db = create_engine('postgresql+psycopg2:...//') meta = MetaData() meta.bind = db 

Person table:

tbl_person = Table(    'person', meta,    Column('id', Integer, Sequence('seq_person_id'), primary_key=True),    Column('name', String(100), unique=True, nullable = False),    Column('password', String(40), nullable = False),    Column('person_type_id', Integer, ForeignKey("person_type.id"), nullable = False),    Column('register_date', DateTime, default = datetime.now),    Column('pendencies', String(200)),    Column('active', Boolean, default = True),    schema = 'public') 

Bug Table:

tbl_estate_agent = Table(    'estate_agent', meta,    Column('person_id', Integer, ForeignKey("person.id"), primary_key = True),    Column('prize_range_id', Integer, ForeignKey("prize_range.id"), nullable = False),    schema = 'public') 

Normal table (creating normally the fk)

tbl_person_agent = Table(    'person_agent', meta,    Column('person_id', Integer, ForeignKey("person.id"), primary_key = True),    Column('prize_range_id', Integer, ForeignKey("prize_range.id"), nullable = False),    schema = 'public') 

Creation Call:

meta.create_all(checkfirst=True) 

Complete error log:

Traceback (most recent call last):    File "database_client.py", line 159, in <module>     meta.create_all(checkfirst=True)    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 3404, in create_all     tables=tables)    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1616, in _run_visitor     conn._run_visitor(visitorcallable, element, **kwargs)    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1245, in _run_visitor     **kwargs).traverse_single(element)    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 120, in traverse_single     return meth(obj, **kw)    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line 699, in visit_metadata     collection = [t for t in sort_tables(tables)    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line 862, in sort_tables     {'foreign_key': visit_foreign_key})    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 256, in traverse     return traverse_using(iterate(obj, opts), obj, visitors)    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 247, in traverse_using     meth(target)    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line 853, in visit_foreign_key     parent_table = fkey.column.table   File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 725, in __get__     obj.__dict__[self.__name__] = result = self.fget(obj)    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 1720, in column tablekey) sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'estate_agent.person_id' could not find table 'person' with which to generate a foreign key to target column 'id' 
like image 745
Diego Moreira Avatar asked Jan 20 '15 13:01

Diego Moreira


People also ask

What is Automap in SQLAlchemy?

Define an extension to the sqlalchemy. ext. declarative system which automatically generates mapped classes and relationships from a database schema, typically though not necessarily one which is reflected.

Is SQLAlchemy worth learning?

SQLAlchemy is the ORM of choice for working with relational databases in python. The reason why SQLAlchemy is so popular is because it is very simple to implement, helps you develop your code quicker and doesn't require knowledge of SQL to get started.

How do I create a database using SQLAlchemy?

First, we import all the requirements from the sqlalchemy library. After that, we create the engine which is used to perform all the operations like creating tables, inserting or modifying values into a table, etc. From the engine, we can create connections on which we can run database queries on.

How do you update existing table rows in SQLAlchemy in Python?

Update table elements in SQLAlchemy. Get the books to table from the Metadata object initialized while connecting to the database. Pass the update query to the execute() function and get all the results using fetchall() function. Use a for loop to iterate through the results.


2 Answers

The solution is to replace the strings with actual columns:

Column('person_id', Integer, ForeignKey(tbl_person.c.id), primary_key=True) 
like image 73
Matthew Moisen Avatar answered Sep 22 '22 00:09

Matthew Moisen


By adding the following line to my parent table solved my problem. In case of Declarative:

children = relationship("Child") 

Otherwise: SQLAlchemy - Classic Mapper

Also try to have a look in here (SO) too, might help.

like image 29
Hamed Avatar answered Sep 21 '22 00:09

Hamed