Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first mistake I can see if in your models-- your association correctly has two foreign keys, but the foreign key for the tag is an <code>Integer</code> but your primary key within your <code>Tag</code> class is a <code>String</code>-- they should match. Apart from that the models look good.</p> <p>You also should be able to simplify your <code>create_product</code> function:</p> <pre><code>def create_product(title, description, image, link, price, expiration, tags): image = imagefolder + image tag_list = [] for tag in tags: tagcheck = Tag.query.filter_by(name=tag).first() if tagcheck is None: tag_list.append(Tag(tag)) else: tag_list.append(tagcheck) product = Product(title, description, image, link, price, datetime.utcnow(), expiration, tag_list) return product </code></pre> <p>This can easily be moved inside your <code>__init___</code> constructor. In your example you deal directly with the association table, but you don't have to at all-- just trust the ORM to do the correct thing, that's the beauty of SQLAlchemy.</p> <p>Here's an example of what your product model could look like:</p> <pre><code>class Product(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128)) description = db.Column(db.Text) image = db.Column(db.String(64)) link = db.Column(db.String(256)) price = db.Column(db.Float()) timestamp = db.Column(db.DateTime) expiration = db.Column(db.String(6)) tags = db.relationship('Tag', secondary=product_tags, backref=db.backref('products', lazy='dynamic')) def __init__(self, title, description, image, link, price, timestamp, expiration, tags): self.title = title self.description = description self.image = image self.link = link self.price = price self.timestamp = timestamp self.expiration = expiration for tag in tags: tagcheck = Tag.query.filter_by(name=tag).first() if tagcheck is None: self.tags.append(Tag(tag)) else: self.tags.append(tagcheck) def __repr__(self): return '&lt;{}, {}&gt;'.format(self.title, ":".join([x.name for x in self.tags])) </code></pre> <p>And to test it out, first lets add a couple of tags to the system:</p> <pre><code>ta = Tag('cat') tb = Tag('dog') db.session.add_all([ta, tb]) db.session.commit() &gt;&gt;&gt; Tag.query.all() [&lt;Tag u'cat'&gt;, &lt;Tag u'dog'&gt;] </code></pre> <p>Now lets add a product that uses those tags, plus a new tag.</p> <pre><code>p = Product( 'title', 'description', 'image', 'link', 0.0, datetime.now(), 'expiry', ['dog','cat','horse'] ) db.session.add(p) db.session.commit() </code></pre> <p>When we create that product, the constructor takes each of those three string tags, and says "Hey, does a tag of this name already exist?" if so, it uses it, and if not, it creates a new tag by that name. SQLAlchemy is clever enough to know to add the new Tag into the session, and to commit it when the product is committed.</p> <pre><code>&gt;&gt;&gt; Tag.query.all() [&lt;Tag u'cat'&gt;, &lt;Tag u'dog'&gt;, &lt;Tag u'horse'&gt;] </code></pre> <p>Now lets find all products with the dog tag (assuming more products have been added).</p> <pre><code>&gt;&gt;&gt; tag = Tag.query.get('dog') &gt;&gt;&gt; products = tag.products &gt;&gt;&gt; [x.title for x in products] ['title','other','examples'] </code></pre> <p>Again, at no point am I touching the association table at all, there's no need. SQLAlchemy is saving us effort.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload