yii2 excel import using arogachev plugin - excel

I am importing excel with column name FirstName, LastName and Username. Username is unique in db and model validation both. I am trying to import excel which has 2 row with same username.
It throws below error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
'a#2.com' for key 'username'
But this error should not come as before importing excelsheet there is no such value in DB. And model validation should detect duplicate value.
Please help

Turns out that there was a bug in basic import with this type of validation because the validation needs to be performed after previous models were saved.
I fixed this and released new version.
Please update to the latest version using command:
composer update arogachev/yii2-excel

Related

Flask-migrate: change model attributes and rename corresponding database columns

I have a bit of experience with Flask but not very much with databases (Flask-migrate / alembic / SqlAlchemy).
I'm following this tutorial and things are working quite alright.
I have a User model like this:
# user_model.py
from app import DB
... other imports
class User(UserMixin, DB.Model):
__tablename__ = 'users'
id = DB.Column(DB.Integer, primary_key=True)
username = DB.Column(DB.String(64), index=True, unique=True)
email = DB.Column(DB.String(120), index=True, unique=True)
password_hash = DB.Column(DB.String(128))
I can then initialize the db, do migrations, upgrades etc.
The problem started when I wanted to change that id attribute, which in Python is not a great variable name choice. Let's say I want to rename that to user_id instead.
Now obviously the db already exists and there is some data inside. I thought maybe by some kind of magic from Flask-Migrate/Alembic just modifying the User class would work. That is just change the id line above to:
user_id = DB.Column(DB.Integer, primary_key=True)
If I do this and run flask db migrate I get:
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.autogenerate.compare] Detected added column 'users.user_id'
INFO [alembic.autogenerate.compare] Detected removed column 'users.id'
So actually Alembic detects this as a column being removed and a new one added, which I suppose makes sense.
But this in fact doesn't work if I run flask db upgrade. I get the following error:
ERROR [alembic.env] (sqlite3.OperationalError) Cannot add a NOT NULL
column with default value NULL [SQL: 'ALTER TABLE users ADD COLUMN
user_id INTEGER NOT NULL']
The error is quite clear. The point is that I don't want to add a new column, I just want to rename an existing one.
Looking around I also tried to modify the script.py handling the upgrade to use the alter_column method:
def upgrade():
${upgrades if upgrades else "pass"}
# just added this line below
op.alter_column('users', 'id', nullable=False, new_column_name='user_id')
However this also doesn't seem to work (I get the same error as above).
So the question boils down to a very simple one: how do I rename a database columns in a Flask app using Flask-Migrate? Or in other words, if I wish to modify the attributes of a given model, what do I have to do so that the corresponding column names in the database are correctly renamed?
To simply rename a column in an alembic script (which is the same as flask-migrate), what you do is correct:
op.alter_column('users', 'id', nullable=False, new_column_name='user_id')
The problem comes from, in my opinion, that you need also to change its constraint as primary key:
op.drop_constraint('user_pkey', 'users', type_='primarykey')
op.create_primary_key('user_pkey', 'users', ['user_id'])
You may need to adjust the name of the primary key you re-create depending of your database type (It works like this for me with PostgreSQL)
Autogenerated alembic scripts should always be reviewed, quite often they do not do what we want if it's not for simple changes.
Note: If your column id was used as a foreign key, you may also want to change the foreign key constraints in other tables.
Alter Primary Key in Alembic describes the same kind of problem.

The entity User is not key value coding-compliant for the key "dateEndSubscription"

I have a swift project with 3 entities in my xcdatamodeld: Access, CustomerInfo and User. I am trying to save the dateEndSubscription separately in the User. When I am trying to save , I get error as : Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: the entity User is not key value coding-compliant for the key "dateEndSubscription".' Anyone please help to solve it in swift4.
The json message contains the key dateEndSubscription but the attribute in your User entity is named dateEnd so they doesn’t match.
A few options to solve this in your saveUser method
Change API.DateEnd to dateEnd but maybe that will infer with the decoding of the json message.
Don't use API key but instead hardcode attribute name
user.setValue(dateEnd, forKey: "dateEnd")
and lastly use the property of the User class directly
user.dateEnd = dateEnd
You need to change saveCustomerInformation as well since you are working with a User object there as well although it's unclear why.

Failed to add fields.Selection to parent model

I'm working on access restriction to res.partner.
Currently I have two questions:
1.Why does this code don't create new field to inherited model (first error was something like "unknown object _", now it's gone): (Now it works, first question is answered)
from odoo import models, fields
class partner(models.Model):
_inherit = 'res.partner'
privacy_visibility = fields.Selection([
('followers', _('On invitation only')),
('employees', _('Visible by all employees')),
#error was here. according to odoo documentation, here is a comma
#if you remove it, the code works: [![enter image description here][1]][1]([('',''),('',''),('','')])
],
string='Privacy', required=True,
default='employees',
help="Holds visibility of the partner that affects currently logged user:\n"
"- On invitation only: Employee may only see the followed partners\n"
"- Visible by all employees: Employee may see selected partner\n")
Later, when my restrictions would be applied, how to make selected users to access all pertners anyway? (my ideas - 1.to hide "privacy_visibility" field to be visible in developer mode only, as it made for project's "sub-task project". 2.to create a new group, but I have no idea how to use access groups with row-level access), which way wold you recommend to go?
First Question Ans:
The Selection field syntax is not correct please follow this syntax
Example:
gender = fields.Selection([('male', 'Male'), ('female', 'Female'), ('any', 'Any')], string='Gender')
You are calling the translate object (the underscore "_") to translate your selection values, but you did not import it.
Change:
from odoo import models, fields
To:
from odoo import models, fields, _

How to set the version fields of a JIRA ticket from Groovy

I'm using JIRA 7.1.4 Server and under Behaviours I'm trying to create a Groovy Initialiser Function for setting default values of form fields, so when the user tries to create a new ticket, some fields are already filled in with default values. This is what I have so far:
import com.atlassian.jira.component.ComponentAccessor
def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersionsUnreleased(issueContext.projectObject.id, false)
getFieldById("affectedVersion").setFormValue([versions.first().id])
getFieldById("description").setFormValue([versions.first().id])
When the dialog for creating a new JIRA ticket opens, this script successfully sets the "Description" field to the right version id (only for debugging purposes), but the "Affects Version/s" field remains empty for some reason.
I think the id of the "Affects Version/s" field is OK, because I got it from JQL, so e.g. the following query displays correct information:
project = "--------" and affectedVersion is EMPTY
Therefore I assume that I'm trying to set the value of the version field incorrectly, but cannot figure out the mistake. The above Groovy script is based on this example, but the script might be wrong, and I was not able to find more information about getFieldById or setFormValue here either.
Can anyone give a working example of setting JIRA's "Affects Version/s" or "Fix Version/s" fields from Groovy?
If you setting fixversion on issue create step in workflow. You need to put this script into first post function(before issue create).
import com.atlassian.jira.component.ComponentAccessor
def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersionsUnreleased(issue.getProjectObject().getId(), false)
issue.setAffectedVersions([versions.first()])

Subsonic BatchQuery.Queue causing 'Can't decide which property to consider the key...' exception

I'm just getting started with Subsonic 3.0 ActiveRecord and am trying to implement a batch query like the one in the SubSonic docs. I'm using a batch so I can query a User and a list of the users Orders in one shot.
When I call the BatchQuery.Queue() method, adding my "select user" query, SubSonic throws the following exception:
System.InvalidOperationException : Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute
The code is as follows:
var db = new MyDB();
var userQuery = from u in db.Users //gets user by uid
where u.uid == 1
select u;
var provider = ProviderFactory.GetProvider();
var batch = new BatchQuery(provider);
batch.Queue(userQuery); //exception here
//create and add "select users orders" query here...
First things first - Why this error? My SubSonic Users object knows it's PK. "uid" is the PK in the database and the generated code reflects this. And I thought SubSonicPrimaryKey attribute was for the SimpleRepository? Is this way of batching not for ActiveRecord?
I could ask a number of other questions, but I'll leave it at that. If anyone can help me figure out what is going on and how to issue 2 batched queries I'd be grateful!
Edit - after further investigation
I ran through the source code with the debugger. Adam is correct - the ToSchemaTable() method in Objects.cs is apparently building out my schema and failing to find a PK. At the very end, it tries to find a column property named "ID" and flags this as the PK, otherwise it throws the exception. I added a check for "UID" and this works!
Still... I'm confused. I'm admittedly a bit lost after peeling back layer after layer of the source, but it seems like this portion of code is trying to build up a schema for my table and completely ignoring my generated User class - which quite nicely identifies which column/property is the PK! It doesn't seem quite right that I'd be required to name all keys "ID" w/ ActiveRecord.
I think the answer you're looking for is that this is a really stupid bug on my part. I'm hoping to push another build next week and if you could put this on the issue list I'd really appreciate it. My apologies...
SubSonic expects your primary key to be called Id so it's getting confused. SubSonicPrimaryKey is for simple repository but I assume where that exception is being thrown is shared between the different templates. If you rename your PK to Id or id or ID your query will work.

Resources