how to set a multi-column unique in web2py - mysql

In order to make many to many relation ship I make a middle table to combine two tables ,the tables like this:
db.define_table('problem',
Field('task_id','reference task'),
Field('title','string',unique=True,length=255))
db.define_table('task',
Field('title','string',unique=True,length=255),
Field('course_id','reference courses'))
db.define_table('belong',
Field('task_id','reference task'),
Field('problem_id','reference problem')
)
db.belong.task_id.requires=IS_IN_DB(db,'task.id','%(title)s')
db.belong.problem_id.requires=IS_IN_DB(db,'problem.id','%(title)s')
I use SQLFORM to insert into belong table.I want there are no duplicate task and problem. suppose there is a record like (1,task1,problem1)already exist in belong table,then if I choose task1 in SQLFORM,how to make the problem_id dropdown list not to show problem1. I have found similar question here
and do some test like the following in db.py:
test1:
db.belong.problem_id.requires=IS_NOT_IN_DB(db(db.belong.task_id==request.vars.task_id),'belong.problem_id').
test2:
db.belong.problem_id.requires=IS_NOT_IN_DB(db(db.belong.task_id==request.vars.task_id),'problem.id','%(title)s').
test3:
def my_form_processing(form):
a = form.vars.task_id
b=form.vars.problem_id
query=(db.belong.task_id==a)&(db.belong.problem_id==b)
if query :
form.errors.a= 'the record has existed'
def assignproblem():
form=SQLFORM(db.belong)
if form.process(onvalidation=my_form_processing).accepted:
response.flash='form accepted'
elif form.errors:
response.flash='form has errors'
else:
response.flash='please fill out the form'
return dict(form=form)
but it still not been solved.

If you want the choices in the problem_id dropdown to change dynamically based on the task_id selection, then you will need to use JavaScript (and likely make an Ajax request to populate the dropdown). For possible solutions, see this answer.
As for your tests above, test1 should do the proper validation, but only after the form has been submitted (i.e., the form will allow any combination of task_id and problem_id to be selected but will then report an error if a duplicate combination has been submitted).
test3 does not work as is because the query object just specifies the database query without actually doing a select from the database. Instead, you must call the .select() method, or more simply, the .count() method:
if db((db.belong.task_id == a) & (db.belong.problem_id == b)).count():
form.errors.a= 'the record has existed'

Related

Rails, MySql, JSON column which stores array of UUIDs - Need to do exact match

I have a model called lists, which has a column called item_ids. item_ids is a JSON column (MySQL) and the column contains array of UUIDs, each referring to one item.
Now when someone creates a new list, I need to search whether there is an existing list with same set of UUIDs, and I want to do this search using query itself for faster response. Also use ActiveRecord querying as much as possible.
How do i achieve this?
item_ids = ["11E85378-CFE8-39F8-89DC-7086913CFD4B", "11E85354-304C-0664-9E81-0A281BE2CA42"]
v = List.new(item_ids: item_ids)
v.save!
Now, how do I check whether a list exists which has item ids exactly matches with that mentioned in query ? Following wont work.
list_count = List.where(item_ids: item_ids).count
Edit 1
List.where("JSON_CONTAINS(item_ids, ?) ", item_ids.to_json).count
This statement works, but it counts even if only one of the item matches. Looking for exact number of items.
Edit 2
List.where("JSON_CONTAINS( item_ids, ?) and JSON_LENGTH(item_ids) = ?", item_ids.to_json, item_ids.size).count
Looks like this is working
You can implement a has many relation between lists and items and then access like this.
List.includes(:item).where('items.id in (?)',item_ids)
To implement has_many relation:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

Contao CMS Query a 'checkboxWizard' BLOB field

I have a question about how to query a 'checkboxWizard' BLOB field. In have added a such field to tl_member. This is working very fine. I can add “0 to N” selection to each members. Let’s call this field “myBlob”.
Now the questions is how to query “myBlob” with the Contao way? Let’s say I want all member that are in the postal code “12120” and that have the id “2” of “myBlob” selected. Not only “2” but at least this one.
$arrColumn[] = "tl_member.postal=?";
$arrValues[] = 12120;
$arrColumn[] = "tl_member.myBlob=?"; <- how to say “contains in the blob” here?
$arrValues[] = 2;
self::findBy($arrColumn, $arrValues)
The only way to do this (when using the default Contao method for such relationships) is to create a query like:
… WHERE myBlob LIKE '%"2"%'
So in your case it might be:
$arrColumn[] = "tl_member.myBlob LIKE ?";
$arrValues[] = '%"2"%';
However, this is of course cumbersome and might not work in all cases.
May be a better way would be to use codefog/contao-haste with its 'many to many' helper: https://github.com/codefog/contao-haste/blob/master/docs/Model/index.md
This way you will have a separate table containing the references.

updating records from controller in web2py

I want to update some field in a record of database form controller given i know which record that is and which field that is.How do i do that?
Eg:In the table 'students' in the database there is field called gender which is either 'male' or 'female'.After adding many records,i add a new field called 'gender_description'.It should be 'girl' when gender is female and 'boy' if gender is male.I want to populate this new field for all the records automatically.How do i achieve this?
In a web2py shell, you can do:
db(db.students.gender == 'male').update(gender_description='boy')
db(db.students.gender == 'female').update(gender_description='girl')
You don't want to do this in the app code, because the above should be run only once. Also, make sure the "gender_description" field has already been added to the model definition and that a migration has been run (this will happen automatically as long as migrations are enabled).
Note, if there is always a one-to-one correspondence between "gender" and "gender_description", this may not be the best database design.
As an alternative, you might consider creating a virtual field in web2py:
db.define_table('students',
Field('gender', ...),
Field.Virtual('gender_description',
lambda r: 'boy' if r.gender == 'male' else 'girl'),
...)

Dynamically selecting MySQL updates

This is more of a theory question. I have a page with a bunch of various textfields, dropdown boxes, etc. Each user has his/her "own page" that can be updated via this update page that I am referring to. He updates the fields at his choosing. It passes about 30 variables (if every field is inputted) to a "preview page". If the person likes the preview page, then they click an "update" button at the bottom of the preview page and all the various variables are updated into the appropriate MySQL table and their "own page" that others see is updated dynamically. (let me know if this explanation isn't clear).
Inserting this information for the first time is easy. However, when a user wants to update only a few of the fields for his page later, this is where I am confused. How do I make the MySQL update query dynamic to recognize an update to ONLY the fields on the page that the user wants to update (while he leaves the other fields blank, thus leaving the old information intact for those columns, and they are disregarded in the update query).
Let me know if what I'm asking doesn't make sense and I'll try again.
Thanks for your help.
The easiest way to do this would be
UPDATE MyTable m SET m.f1 = COALESCE(input1,m.f1), m.f2 = COALESCE(input2,m.f2), ....
WHERE m.id = key;
The COALESCE will return the first non-null value (or null if all values are null).
Note that you can insert a default value after the existing field value if you want to force a default.
Like so:
UPDATE MyTable m SET m.f1 = COALESCE(input1,m.f1,default1), m.f2 = COALESCE(input2,m.f2,default2), ....
WHERE m.id = key;
See: MySQL: how to use COALESCE
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce

Can I create sperate queries for different views?

I'm learning sqlalchemy and not sure if I grasp it fully yet(I'm more used to writing queries by hand but I like the idea of abstracting the queries and getting objects). I'm going through the tutorial and trying to apply it to my code and ran into this part when defining a model:
def __repr__(self):
return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)
Its useful because I can just search for a username and get only the info about the user that I want but is there a way to either have multiple of these type of views that I can call? or am I using it wrong and should be writing a specific query for getting different data for different views?
Some context to why I'm asking my site has different templates, and most pages will just need the usersname, first/last name but some pages will require things like twitter or Facebook urls(also fields in the model).
First of all, __repr__ is not a view, so if you have a simple model User with defined columns, and you query for a User, all the columns will get loaded from the database, and not only those used in __repr__.
Lets take model Book (from the example refered to later) as a basis:
class Book(Base):
book_id = Column(Integer, primary_key=True)
title = Column(String(200), nullable=False)
summary = Column(String(2000))
excerpt = Column(Text)
photo = Column(Binary)
The first option to skip loading some columns is to use Deferred Column Loading:
class Book(Base):
# ...
excerpt = deferred(Column(Text))
photo = deferred(Column(Binary))
In this case when you execute query session.query(Book).get(1), the photo and excerpt columns will not be loaded until accessed from the code, at which point another query against the database will be executed to load the missing data.
But if you know before you query for the Book that you need the column photo immediately, you can still override the deferred behavior with undefer option: query = session.query(Book).options(undefer('photo')).get(1).
Basically, the suggestion here is to defer all the columns (in your case: except username, password etc) and in each use case (view) override with undefer those you know you need for that particular view. Please also see the group parameter of deferred, so that you can group the attributes by use case (view).
Another way would be to query only some columns, but in this case you are getting the tuple instance instead of the model instance (in your case User), so it is potentially OK for form filling, but not so good for model validation: session.query(Book.id, Book.title).all()