croach / Flask-Fixtures

A simple library for adding database fixtures for unit tests using nothing but JSON or YAML.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Many-to-many issue

sobolevn opened this issue · 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?

After running tests with my fix:

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