Many-to-Many Association Object SQLAlchemy Issues










0















I'm back with another SQL Alchemy question..



So I have a many-to-many relationship between two classes: Dataset and Column (ignore the obvious horrible names).



I was using an association table before and everything was working great. But I now need to add an extra column to that association table so I had to convert the table to an object.



I'm following the docs but I keep getting this error:



AssertionError: Attribute 'datasets' on class '<class 'test_models.Table'>' doesn't handle objects of type '<class 'test_models.Dataset'>'


My Setup



class Dataset(BASE):
__tablename__ = 'dataset'

dataset_id = Column(INTEGER(10), primary_key=True, unique=True, nullable=False)
dataset_name = Column(Text, nullable=False)
dataset_title = Column(Text)
dataset_comment = Column(Text)

tables = relationship('Dataset_Table',
back_populates='datasets')


class Table(BASE):
__tablename__ = 'table'

table_id = Column(INTEGER(11), primary_key=True, unique=True, nullable=False)

table_name = Column(Text, nullable=False)
table_group = Column(Text)
table_description = Column(Text)

table_longitudinal = Column(Enum('S', 'Y', 'N', 'L'))

datasets = relationship('Dataset_Table',
back_populates='tables')


Here's is a link to the full stack trace: https://pastebin.com/NHgLCWq6



Is my approach incorrect? What am I doing wrong? I simply want my many-to-many to work with the associative table as before with the Association Table but with an Association Object



EDIT:



I apologize, I didn't include the association table linking the two tables above together:



class Dataset_Table(BASE):
__tablename__ = 'dataset_table'

id = Column('id', INTEGER, primary_key=True, unique=True, nullable=False)
dataset_id = Column('dataset_id', INTEGER, ForeignKey('dataset.dataset_id'), nullable=False)
table_id = Column('table_id', INTEGER, ForeignKey('table.table_id'), nullable=False)

position = Column(INTEGER, nullable=False)

datasets = relationship('Dataset',
back_populates='tables')

tables = relationship('Table',
back_populates='datasets')


As you can see, this is a standard many-to-many with back population like this



Im putting the full stack trace below:



 Traceback (most recent call last):
File "/Users/kevin.dasilva/SQL_tings/SQL_ingest.py", line 103, in <module>
sess.add_all(objs)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1785, in add_all
self.add(instance, _warn=False)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1776, in add
self._save_or_update_state(state)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1795, in
_save_or_update_state
halt_on=self._contains_state):
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/mapper.py", line 2870, in cascade_iterator
visited_states, halt_on))
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/relationships.py", line 1591, in
cascade_iterator
c.__class__
AssertionError: Attribute 'datasets' on class '<class
'test_models.Table'>' doesn't handle objects of type '<class
'test_models.Dataset'>'


EDIT:



I believe I am searching for a "bidirectional many to many" with an association object










share|improve this question
























  • Hi Kevin, I'd like to be able to help but you haven't included enough code for me to reproduce the issue. Please include a Minimal, Complete and Verifiable Example. From the error message it looks like you are trying to add instances of test_models.Dataset to Table.datasets but that relationship is defined to handle Dataset_Table instances (and that model isn't included in your example code). Also, include the stack trace in the body of your question as opposed to an external link (and the stack trace in the link doesn't look to be complete).

    – SuperShoot
    Nov 13 '18 at 9:04











  • Thank you for your feedback! I totally forgot a vital model there.. Im going to add everything now.

    – Kevin Dasilva
    Nov 13 '18 at 18:07











  • That’s still not a MCVE. Where is the code that includes this line: sess.add_all(objs)? That is where the error is originating from.

    – SuperShoot
    Nov 14 '18 at 0:48















0















I'm back with another SQL Alchemy question..



So I have a many-to-many relationship between two classes: Dataset and Column (ignore the obvious horrible names).



I was using an association table before and everything was working great. But I now need to add an extra column to that association table so I had to convert the table to an object.



I'm following the docs but I keep getting this error:



AssertionError: Attribute 'datasets' on class '<class 'test_models.Table'>' doesn't handle objects of type '<class 'test_models.Dataset'>'


My Setup



class Dataset(BASE):
__tablename__ = 'dataset'

dataset_id = Column(INTEGER(10), primary_key=True, unique=True, nullable=False)
dataset_name = Column(Text, nullable=False)
dataset_title = Column(Text)
dataset_comment = Column(Text)

tables = relationship('Dataset_Table',
back_populates='datasets')


class Table(BASE):
__tablename__ = 'table'

table_id = Column(INTEGER(11), primary_key=True, unique=True, nullable=False)

table_name = Column(Text, nullable=False)
table_group = Column(Text)
table_description = Column(Text)

table_longitudinal = Column(Enum('S', 'Y', 'N', 'L'))

datasets = relationship('Dataset_Table',
back_populates='tables')


Here's is a link to the full stack trace: https://pastebin.com/NHgLCWq6



Is my approach incorrect? What am I doing wrong? I simply want my many-to-many to work with the associative table as before with the Association Table but with an Association Object



EDIT:



I apologize, I didn't include the association table linking the two tables above together:



class Dataset_Table(BASE):
__tablename__ = 'dataset_table'

id = Column('id', INTEGER, primary_key=True, unique=True, nullable=False)
dataset_id = Column('dataset_id', INTEGER, ForeignKey('dataset.dataset_id'), nullable=False)
table_id = Column('table_id', INTEGER, ForeignKey('table.table_id'), nullable=False)

position = Column(INTEGER, nullable=False)

datasets = relationship('Dataset',
back_populates='tables')

tables = relationship('Table',
back_populates='datasets')


As you can see, this is a standard many-to-many with back population like this



Im putting the full stack trace below:



 Traceback (most recent call last):
File "/Users/kevin.dasilva/SQL_tings/SQL_ingest.py", line 103, in <module>
sess.add_all(objs)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1785, in add_all
self.add(instance, _warn=False)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1776, in add
self._save_or_update_state(state)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1795, in
_save_or_update_state
halt_on=self._contains_state):
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/mapper.py", line 2870, in cascade_iterator
visited_states, halt_on))
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/relationships.py", line 1591, in
cascade_iterator
c.__class__
AssertionError: Attribute 'datasets' on class '<class
'test_models.Table'>' doesn't handle objects of type '<class
'test_models.Dataset'>'


EDIT:



I believe I am searching for a "bidirectional many to many" with an association object










share|improve this question
























  • Hi Kevin, I'd like to be able to help but you haven't included enough code for me to reproduce the issue. Please include a Minimal, Complete and Verifiable Example. From the error message it looks like you are trying to add instances of test_models.Dataset to Table.datasets but that relationship is defined to handle Dataset_Table instances (and that model isn't included in your example code). Also, include the stack trace in the body of your question as opposed to an external link (and the stack trace in the link doesn't look to be complete).

    – SuperShoot
    Nov 13 '18 at 9:04











  • Thank you for your feedback! I totally forgot a vital model there.. Im going to add everything now.

    – Kevin Dasilva
    Nov 13 '18 at 18:07











  • That’s still not a MCVE. Where is the code that includes this line: sess.add_all(objs)? That is where the error is originating from.

    – SuperShoot
    Nov 14 '18 at 0:48













0












0








0








I'm back with another SQL Alchemy question..



So I have a many-to-many relationship between two classes: Dataset and Column (ignore the obvious horrible names).



I was using an association table before and everything was working great. But I now need to add an extra column to that association table so I had to convert the table to an object.



I'm following the docs but I keep getting this error:



AssertionError: Attribute 'datasets' on class '<class 'test_models.Table'>' doesn't handle objects of type '<class 'test_models.Dataset'>'


My Setup



class Dataset(BASE):
__tablename__ = 'dataset'

dataset_id = Column(INTEGER(10), primary_key=True, unique=True, nullable=False)
dataset_name = Column(Text, nullable=False)
dataset_title = Column(Text)
dataset_comment = Column(Text)

tables = relationship('Dataset_Table',
back_populates='datasets')


class Table(BASE):
__tablename__ = 'table'

table_id = Column(INTEGER(11), primary_key=True, unique=True, nullable=False)

table_name = Column(Text, nullable=False)
table_group = Column(Text)
table_description = Column(Text)

table_longitudinal = Column(Enum('S', 'Y', 'N', 'L'))

datasets = relationship('Dataset_Table',
back_populates='tables')


Here's is a link to the full stack trace: https://pastebin.com/NHgLCWq6



Is my approach incorrect? What am I doing wrong? I simply want my many-to-many to work with the associative table as before with the Association Table but with an Association Object



EDIT:



I apologize, I didn't include the association table linking the two tables above together:



class Dataset_Table(BASE):
__tablename__ = 'dataset_table'

id = Column('id', INTEGER, primary_key=True, unique=True, nullable=False)
dataset_id = Column('dataset_id', INTEGER, ForeignKey('dataset.dataset_id'), nullable=False)
table_id = Column('table_id', INTEGER, ForeignKey('table.table_id'), nullable=False)

position = Column(INTEGER, nullable=False)

datasets = relationship('Dataset',
back_populates='tables')

tables = relationship('Table',
back_populates='datasets')


As you can see, this is a standard many-to-many with back population like this



Im putting the full stack trace below:



 Traceback (most recent call last):
File "/Users/kevin.dasilva/SQL_tings/SQL_ingest.py", line 103, in <module>
sess.add_all(objs)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1785, in add_all
self.add(instance, _warn=False)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1776, in add
self._save_or_update_state(state)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1795, in
_save_or_update_state
halt_on=self._contains_state):
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/mapper.py", line 2870, in cascade_iterator
visited_states, halt_on))
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/relationships.py", line 1591, in
cascade_iterator
c.__class__
AssertionError: Attribute 'datasets' on class '<class
'test_models.Table'>' doesn't handle objects of type '<class
'test_models.Dataset'>'


EDIT:



I believe I am searching for a "bidirectional many to many" with an association object










share|improve this question
















I'm back with another SQL Alchemy question..



So I have a many-to-many relationship between two classes: Dataset and Column (ignore the obvious horrible names).



I was using an association table before and everything was working great. But I now need to add an extra column to that association table so I had to convert the table to an object.



I'm following the docs but I keep getting this error:



AssertionError: Attribute 'datasets' on class '<class 'test_models.Table'>' doesn't handle objects of type '<class 'test_models.Dataset'>'


My Setup



class Dataset(BASE):
__tablename__ = 'dataset'

dataset_id = Column(INTEGER(10), primary_key=True, unique=True, nullable=False)
dataset_name = Column(Text, nullable=False)
dataset_title = Column(Text)
dataset_comment = Column(Text)

tables = relationship('Dataset_Table',
back_populates='datasets')


class Table(BASE):
__tablename__ = 'table'

table_id = Column(INTEGER(11), primary_key=True, unique=True, nullable=False)

table_name = Column(Text, nullable=False)
table_group = Column(Text)
table_description = Column(Text)

table_longitudinal = Column(Enum('S', 'Y', 'N', 'L'))

datasets = relationship('Dataset_Table',
back_populates='tables')


Here's is a link to the full stack trace: https://pastebin.com/NHgLCWq6



Is my approach incorrect? What am I doing wrong? I simply want my many-to-many to work with the associative table as before with the Association Table but with an Association Object



EDIT:



I apologize, I didn't include the association table linking the two tables above together:



class Dataset_Table(BASE):
__tablename__ = 'dataset_table'

id = Column('id', INTEGER, primary_key=True, unique=True, nullable=False)
dataset_id = Column('dataset_id', INTEGER, ForeignKey('dataset.dataset_id'), nullable=False)
table_id = Column('table_id', INTEGER, ForeignKey('table.table_id'), nullable=False)

position = Column(INTEGER, nullable=False)

datasets = relationship('Dataset',
back_populates='tables')

tables = relationship('Table',
back_populates='datasets')


As you can see, this is a standard many-to-many with back population like this



Im putting the full stack trace below:



 Traceback (most recent call last):
File "/Users/kevin.dasilva/SQL_tings/SQL_ingest.py", line 103, in <module>
sess.add_all(objs)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1785, in add_all
self.add(instance, _warn=False)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1776, in add
self._save_or_update_state(state)
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/session.py", line 1795, in
_save_or_update_state
halt_on=self._contains_state):
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/mapper.py", line 2870, in cascade_iterator
visited_states, halt_on))
File "/Users/kevin.dasilva/SQL_tings/SQL_env/lib/python3.6/site-
packages/sqlalchemy/orm/relationships.py", line 1591, in
cascade_iterator
c.__class__
AssertionError: Attribute 'datasets' on class '<class
'test_models.Table'>' doesn't handle objects of type '<class
'test_models.Dataset'>'


EDIT:



I believe I am searching for a "bidirectional many to many" with an association object







python sqlalchemy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 19:35







Kevin Dasilva

















asked Nov 12 '18 at 22:45









Kevin DasilvaKevin Dasilva

112




112












  • Hi Kevin, I'd like to be able to help but you haven't included enough code for me to reproduce the issue. Please include a Minimal, Complete and Verifiable Example. From the error message it looks like you are trying to add instances of test_models.Dataset to Table.datasets but that relationship is defined to handle Dataset_Table instances (and that model isn't included in your example code). Also, include the stack trace in the body of your question as opposed to an external link (and the stack trace in the link doesn't look to be complete).

    – SuperShoot
    Nov 13 '18 at 9:04











  • Thank you for your feedback! I totally forgot a vital model there.. Im going to add everything now.

    – Kevin Dasilva
    Nov 13 '18 at 18:07











  • That’s still not a MCVE. Where is the code that includes this line: sess.add_all(objs)? That is where the error is originating from.

    – SuperShoot
    Nov 14 '18 at 0:48

















  • Hi Kevin, I'd like to be able to help but you haven't included enough code for me to reproduce the issue. Please include a Minimal, Complete and Verifiable Example. From the error message it looks like you are trying to add instances of test_models.Dataset to Table.datasets but that relationship is defined to handle Dataset_Table instances (and that model isn't included in your example code). Also, include the stack trace in the body of your question as opposed to an external link (and the stack trace in the link doesn't look to be complete).

    – SuperShoot
    Nov 13 '18 at 9:04











  • Thank you for your feedback! I totally forgot a vital model there.. Im going to add everything now.

    – Kevin Dasilva
    Nov 13 '18 at 18:07











  • That’s still not a MCVE. Where is the code that includes this line: sess.add_all(objs)? That is where the error is originating from.

    – SuperShoot
    Nov 14 '18 at 0:48
















Hi Kevin, I'd like to be able to help but you haven't included enough code for me to reproduce the issue. Please include a Minimal, Complete and Verifiable Example. From the error message it looks like you are trying to add instances of test_models.Dataset to Table.datasets but that relationship is defined to handle Dataset_Table instances (and that model isn't included in your example code). Also, include the stack trace in the body of your question as opposed to an external link (and the stack trace in the link doesn't look to be complete).

– SuperShoot
Nov 13 '18 at 9:04





Hi Kevin, I'd like to be able to help but you haven't included enough code for me to reproduce the issue. Please include a Minimal, Complete and Verifiable Example. From the error message it looks like you are trying to add instances of test_models.Dataset to Table.datasets but that relationship is defined to handle Dataset_Table instances (and that model isn't included in your example code). Also, include the stack trace in the body of your question as opposed to an external link (and the stack trace in the link doesn't look to be complete).

– SuperShoot
Nov 13 '18 at 9:04













Thank you for your feedback! I totally forgot a vital model there.. Im going to add everything now.

– Kevin Dasilva
Nov 13 '18 at 18:07





Thank you for your feedback! I totally forgot a vital model there.. Im going to add everything now.

– Kevin Dasilva
Nov 13 '18 at 18:07













That’s still not a MCVE. Where is the code that includes this line: sess.add_all(objs)? That is where the error is originating from.

– SuperShoot
Nov 14 '18 at 0:48





That’s still not a MCVE. Where is the code that includes this line: sess.add_all(objs)? That is where the error is originating from.

– SuperShoot
Nov 14 '18 at 0:48












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271190%2fmany-to-many-association-object-sqlalchemy-issues%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271190%2fmany-to-many-association-object-sqlalchemy-issues%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo