Flask-Fixtures icon indicating copy to clipboard operation
Flask-Fixtures copied to clipboard

Many-to-many issue

Open sobolevn opened this issue 9 years ago • 1 comments

I have faced an issue with Flask's db.Table. Here's my setup:

# models.py:
class Project(db.Model):
        id = db.Column(db.Integer, primary_key=True)
    interviewers = db.relationship('User', secondary='project_interviewer')

project_interviewer = db.Table(
    'project_interviewer',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('project_id', db.Integer, db.ForeignKey('project.id')),
)

Now I am trying to load project from the fixtures:

[{
  "model": "merku.models.Project",
  "records": [
    { "id": 1 }
  ]
}, {
  "table": "project_interviewer",
  "records": [
    {
      "id": 1,
      "project_id": 1,
      "user_id": 2
    }
  ]
}]

It was not loading. After some digging I have found out that the issue was here:

# flask_fixtures/__init__.py line: 82
table = Table(fixture['table'], metadata)

After looking inside this object, it became clear, that it was not creating any columns. So, the generated query was: INSERT INTO project_interviewer () VALUES ().

I have made this to work with:

table = Table(fixture['table'], metadata,
                      autoload=True, autoload_with=db.engine)
for record in fixture['records']:  
    # there was an issue 'default engine does not support multi-line inserts':
    conn.execute(table.insert(), record)

Maybe I have missed something?

sobolevn avatar Dec 16 '15 13:12 sobolevn

After running tests with my fix:

___________________________________ summary ____________________________________ ERROR: py26: InterpreterNotFound: python2.6 py27: commands succeeded py34: commands succeeded

sobolevn avatar Dec 16 '15 16:12 sobolevn