Sequelize (v1.5) and node - mysql

How can I check whether a field exists in my table, via sequelize orm.
Note that I have already defined the full object model. I just need to check whether a particular field exists or not.

You can see what is inside your database via:
sequelize.getQueryInterface().describeTable('nameOfTableHere').success(function(data){})
If you want to check the table of a specific model you could also do this:
sequelize.getQueryInterface().describeTable(Model.tableName).success(function(data) {})

Since I had already defined the object model, the following expression gives an array of field names defined in the model.
Object.keys(Model.rawAttributes)

Related

Function field for many2many odoo 8

I need to declare a many2many field as functional field. I tried below code but doesn't create any relational table in database.
def _get_function(self,cr,uid,ids,name,args,context=None):
resp={}
for data in self.browse(cr,uid,ids):
print'inside get Function'
return resp
'many2many_ids': fields.function(_get_function, method=True, relation="table.table1", obj='table1_table2_rel', type="many2many" , string="Many2Many")
Now data saved in form view, but i can't access that values in another function. like,
for data in self.browse(cr,uid,ids):
print'many2many_ids',data.many2many_ids
Here print no values.
How can i do that?
if you want the field to be stored use "store" option.
Also, you might consider using new API on v8. Check "Computed field" in official docs https://www.odoo.com/documentation/8.0/reference/orm.html

cakephp retrive data from one table excluding the associated tables

I am struggling with a basic problem. i am using cake php 2.5. i try to apply the find query in the company model and receiving all the data from companies and with its associations, but i only want to receive the data from company table and want to exclude the data from rest of relationships, can anyone help me with this. below are my queries.
$this->loadModel('Company');
$fields=array('id','name','logo','status');
$conditions=array('status'=>1);
$search_companies = $this->Company->find('first',
compact(array('conditions'=>$conditions,'fields'=>$fields)));
print_r($search_companies);die();
echo json_encode($search_companies);die();
With out seeing your data output, I am just going to take a stab at the problem.
Inside your $search_companies variable you are getting a multidimensional array probably with the other values of the other tables.
Why not just select the one array:
$wantedData = $search_companies['Company'];
// The key Company (which is the model) should be the data you are wanting.
Try setting model's recursive value to -1
$this->Company->recursive = -1;
$search_companies = $this->Company->find('first',
compact(array('conditions'=>$conditions,'fields'=>$fields)));
With this you will not fire the joins queries and therefore you only retrieve model's information.
Cakephp provide this functionality that we can unblind few/all associations on a any model. the keyword unbindModel is used for this purpose. inside the unblindModel you can define the association type and model(s) name that you want to unblind for that specific association.
$this->CurrentModelName->unbindModel(array('AssociationName' => array('ModelName_Youwwant_unblind')));

How to avoid triggering unique validator on model edition?

I'm using Flask, SQLAlchemy and WTForms. I have a number of properties in my model object which are marked as unique and nullable=False. This works fine when creating a new row in the database but when I try to edit an existing object the validator on WTForms fails with
{'aproperty': [u'Already exists.']}
How can I make this validation pass without having to change my data model?
Update
Following the documentation was of no use to me.
You need to associate the existing record with the form. Otherwise the validator has no way of knowing that you're updating an existing record instead of creating a new one. Something like the following should do the trick:
current_obj = ...
form = MyForm(request.form, obj=current_obj)
form.validate_on_submit():
form.populate_obj(current_obj)

SQLAlchemy: exclude few columns during column reflection?

I am mapping a table using declarative base and reflection. The db table has 1k+ columns, but I want to map only few hundred columns whose names are available via a sql.
Using reflection, I get the Column info in my event handler function, which allows me modify the Column's attributes but I am unable to skip the column from mapping.
def column_reflect(inspector, table, column_info):
#...
class MYCLASS1(Base):
__table__ = Table('MYTABLE1', mymetadata, autoload_with=myengine, autoload=True, listeners=[('column_reflect', column_reflect)])
Does SQLAlchemy support skipping certain columns while using reflection?
SQLA version: 0.83 and 0.9.0b1.
'van' answered my question so marking the question answered.
The solution was to use include_columns parameter on Table().

Yii - using MySQL AS clause field

Let's say I want to have a provide CActiveDataProvider for a CGridView. I need to put a SUM(invitesCount) AS invites into a Provider result. How to retrieve it? I guess I cannot just use $dataProvider->invites?
You need to specify the following in your relationinvites
'invites '=>array(self::BELONGS_TO, 'CampaignFund', 'campaign_id', 'select' => 'SUM(invitesCount)'),
and use this relation in your criteria.
Several other options:
Use CStatRelation
invitesCount=>array(self::STAT,'Invites','foreign_key_field');
The addition of a public property can work. However, the field would only be set if you altered the default find query to include this new condition. This can be done by overriding defaultScope() or creating a new scope and using it whenever invitesCount is required.
Another option would be to create a database view from the required query and create a new Model from that database view.