How to make a column be unique?

Forum for posting problems using QxOrm library

How to make a column be unique?

Postby phonix » Sun May 10, 2020 6:49 am

I'm creating a SQLite table using qxOrm. I want a column data be unique (not the primary column). How can I do that?
Thank you very much!
phonix
 
Posts: 6
Joined: Mon Mar 30, 2020 4:04 am

Re: How to make a column be unique?

Postby qxorm » Wed May 13, 2020 8:52 am

Hello,
Sorry I don't understand : how do you create your table exactly ?
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: How to make a column be unique?

Postby phonix » Thu May 14, 2020 3:21 am

I created the table as tutorial:
Code: Select all
#ifndef _PERSON_H_
#define _PERSON_H_

class person
{
public:
   long id;
   QString firstName;
   QString lastName;
   QDateTime birthDate;

   person() : id(0) { ; }
   virtual ~person() { ; }
};

QX_REGISTER_HPP_MY_TEST_EXE(person, qx::trait::no_base_class_defined, 0)

#endif // _PERSON_H_


person.cpp:
Code: Select all
#include "precompiled.h"   // Precompiled-header with '#include <QxOrm.h>' and '#include "export.h"'
#include "person.h"          // Class definition 'person'
#include <QxOrm_Impl.h>     // Automatic memory leak detection and boost serialization export macro

QX_REGISTER_CPP_MY_TEST_EXE(person)   // This macro is necessary to register 'person' class in QxOrm context

namespace qx {
template <> void register_class(QxClass<person> & t)
{
  t.setName("t_person");               // 'person' C++ class is mapped to 't_person' database table

  t.id(& person::id, "id");               // Register 'person::id' <=> primary key in your database
  t.data(& person::firstName, "first_name");      // Register 'person::firstName' property mapped to 'first_name' database column name
  t.data(& person::lastName, "last_name");  // Register 'person::lastName' property mapped to 'last_name' database column name
  t.data(& person::birthDate, "birth_date");  // Register 'person::birthDate' property mapped to 'birth_date' database column name
}}


in main.cpp:
Code: Select all
qx::dao::create_table<person>(&dbConnection);


The table was created. Now I want to make to first_name must be unique. How can I do that? Thanks!
phonix
 
Posts: 6
Joined: Mon Mar 30, 2020 4:04 am

Re: How to make a column be unique?

Postby qxorm » Thu May 14, 2020 12:41 pm

qx::dao::create_table<person>(&dbConnection);

This is explained in the QxOrm manual here : https://www.qxorm.com/qxorm_en/manual.html#manual_470
QxOrm library doesn't provide a generator to create and to update automatically tables into database.
Indeed, qx::dao::create_table<T> function must be used only to create prototypes or samples.


So I would recommend to :
- Use QxEntityEditor application to generate your DDL database schema (which supports UNIQUE)
- Or use a dedicated tool for your SGBD
- Or write your own create_table function for example using introspection engine of QxOrm library (UNIQUE could be defined as a meta-data)
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron