silverqx / TinyORM

Modern C++ ORM library
https://www.tinyorm.org
MIT License
210 stars 22 forks source link

Model implementing belongsToMany not compiling #43

Closed Redmanacac closed 2 months ago

Redmanacac commented 2 months ago

The Model Structure example provided in https://www.tinyorm.org/tinyorm/relationships#many-to-many does not compile.

..hasrelationships.hpp:561: error: C2679: binary '=': no operator found which takes a right-hand operand of type 'std::optional<Derived>' (or there is no acceptable conversion) The example you provided was not that easy to compile for me because of some dependencies. I copied the model classes into my project and still get the same error as stated above.

silverqx commented 2 months ago

If I don't see an exact example code I can't tell where the problem can be.

Redmanacac commented 2 months ago
#pragma once
#ifndef MODELS_USER_HPP
#define MODELS_USER_HPP

#include <QStringList>

#include <orm/tiny/model.hpp>

namespace Models {

using Orm::Tiny::Model;
using Orm::Tiny::Relations::Pivot;

// NOLINTNEXTLINE(misc-no-recursion)
class Role;

class User final : public Model<User, Role, Pivot> {
   friend Model;
   using Model::Model;

 public:
   /*! The roles that belong to the user. */
   std::unique_ptr<BelongsToMany<User, Role>>
   roles() {
      return belongsToMany<Role>();
   }

 private:
   /*! Map of relation names to methods. */
   QHash<QString, RelationVisitor> u_relations{
       {"roles", [](auto& v) { v(&User::roles); }},
   };
};

class Role final : public Model<Role, User, Pivot> {
   friend Model;
   using Model::Model;

 public:
   /*! The users that belong to the role. */
   std::unique_ptr<BelongsToMany<Role, User>>
   users() {
      return belongsToMany<User>();
   }

 private:
   /*! Map of relation names to methods. */
   QHash<QString, RelationVisitor> u_relations{
       {"users", [](auto& v) { v(&Role::users); }},
   };
};
}  // namespace Models

#endif  // MODELS_USER_HPP
silverqx commented 2 months ago

You are missing the #include:

#include <orm/tiny/relations/pivot.hpp>

This includes like pivot.hpp or basepivot.hpp are not included inside the orm/tiny/model.hpp, they can't be because you maybe don't need them.

Redmanacac commented 2 months ago

I just added the missing include, and the error persist.

If I do this, it compiles

class User final : public Model<User, Role, Pivot> {
   friend Model;
   using Model::Model;

 public:
   // /*! The roles that belong to the user. */
   // std::unique_ptr<BelongsToMany<User, Role>>
   // roles() {
   //    return belongsToMany<Role>();
   // }

 private:
   // /*! Map of relation names to methods. */
   // QHash<QString, RelationVisitor> u_relations{
   //     {"roles", [](auto& v) { v(&User::roles); }},
   // };
};
silverqx commented 2 months ago

For me it compiles on msvc, can you post the rest of your code?

silverqx commented 2 months ago

What OS, compiler and version do you use? Also try to #include <optional>.

Redmanacac commented 2 months ago

Compiler: Microsoft Visual C++ Compiler 17.7.34024.191 (amd64)

There is no other code to the models except the one I posted.

What else could be of relevance to that?

Edit: #include <optional> made no difference

silverqx commented 2 months ago

Paste a whole file here I try to compile it on my side.

silverqx commented 2 months ago

What else could be of relevance to that?

I don't know code looks correct, maybe its in build system, pack whole folder with source and build system and I try it to recompile if I find something.

Redmanacac commented 2 months ago

Ah, well. Mutually including the headers of the other table breaks it. Moved it all into one file and now it compiles.

silverqx commented 2 months ago

Ok, if you don't know how to call something you can also inspire from tst_model_relations.cpp, tst_relations_buildsqueries.cpp, tst_relations_inserting_updating.cpp, tst_queriesrelationships.cpp, huge amount of these API-s is unit and functional tested.