How do I insert data from a reference table to the corresponding field - mysql

I'm sorry if my question sounds confusing.I just started learning web2py recently,in this exercise I'm trying to make a simple users management webpage with the admin can assign the users theirs work lists,note and deadline
db.define_table('auth_manager',Field('name','string',requires=IS_NOT_EMPTY()))
db.define_table('manager',Field('user','string','reference user.name'),
Field('workname','text',requires=IS_NOT_EMPTY()),
Field('deadline','date'),)
db.manager.deadline.requires=IS_DATE_IN_RANGE(format=T('%Y-%m-%d'),
minimum=now,maximum=now+datetime.timedelta(60))
I thought of adding the manager's username in auth_manager table using appadmin's new record function.This is my user table
db.define_table('user',Field('name','string',requires=IS_NOT_EMPTY()),
Field('password','password'),
Field('workname','text'),
Field('deadline','date'),
format='%(name)s')
I wanted to insert workname and deadline into user table right after I add those form on manager but I couldn't find any other methods except the update or update_or_insert functions but both don't work because those fields can't be empty and their ids aren't the same value and multiple references to a single table don't work .
One last question,I want to use web2py's RBAC but the first & last name fields are often unnecessary if I want to use a full name field is there other way to do it?
Sorry for the long post,I hope I made my question clear.

You can use the tables from auth and let web2py to handle everything in between.
The following code should resolve your problem:
db.define_table('manager', 'reference auth_user'),
Field('workname', 'text', requires=IS_NOT_EMPTY()),
Field('deadline', 'date'))

Related

How to specify Microsoft Access auto number prefix value based on user selection

I am currently experimenting with Microsoft Access and was curious how one would accomplish this.
I know that one can add a prefix to an autonumber in the format option such as "EMP"000, and each autonumber would fill as EMP001, EMP002, and so on.
What I would like to do is have the "EMP" change dynamically so if my personnel table has them as a Manager for example, it would be listed as MGR003, MGR004, etc.
My thought was to have something like the following in the format section but again am still new to indepth access so please excuse my crude write up.
"SELECT FROM [PersonnelTable].[PersonnelAbbreviation] if [Add Task].[AddTaskPersonnelType]==[PersonnelTable].[PersonnelType];"000
So to recap, I have two tables one "Add Task" the other "Personnel". Would like prefix on Add Task AutoNumber to be based off the abbreviation I have in table Personnel.
Thank you Stack Overflow users!
Simply use a query (air code)
SELECT anydesiredfields, PersonellTable.PersonellType & Format(PersonellTable.PersonellID, "000") as FormattedID
FROM PersonellTable
INNER JOIN AddTask ON PersonellTable.PersonellID = AddTask.PersonellID

Yii2 is there a way to specify tablename in ActiveQuery conditions (like andWhere) in a nice and short way

I make a query (with \yii\db\ActiveQuery) with joins, and some fields in "where" clause become ambigous. Is there a nice and short way to specify the name of the current model`s (ActiveRecord) table (from which one the ActiveQuery was instantiated) before the column name? So I can use this all the time in all cases and to make it short.
Don't like doing smth like this all the time (especially in places where there're no joins, but just to be able to use those methods with joins if it will be needed):
// in the ActiveQuery method initialized from the model with tableName "company"
$this->andWhere(['{{%company}}.`company_id`' => $id]);
To make the "named scopes" to work for some cases with joins..
Also, what does the [[..]] mean in this case, like:
$this->andWhere(['[[company_id]]' => $id]);
Doesn't seem to work like to solve the problem described above.
Thx in advance!
P.S. sorry, don't have enough reputation to create tag yii2-active-query
to get real table name :
Class :
ModelName::getTableSchema()->fullName
Object :
$model::getTableSchema()->fullName
Your problem is a very common one and happens most often with fields liek description, notes and the like.
Solution
Instead of
$this->andWhere(['description'=>$desc]);
you simply write
$this->andWhere(['mytable.description'=>$desc]);
Done! Simply add the table name in front of the field. Both the table name and the field name will be automatically quoted when the raw SQL is created.
Pitfall
The above example solves your problem within query classes. One I struggled over and took me quite some time to solve was a models relations! If you join in other tables during your queries (more than just one) you could also run into this problem because your relation-methods within the model are not qualified.
Example: If you have three tables: student, class, and teacher. Student and teacher probably are in relation with class and both have a FK-field class_id. Now if you go from student via class to teacher ($student->class->teacher). You also get the ambigous-error. The problem here is that you should also qualify your relation definitions within the models!
public function getTeacher()
{
return $this->hasOne(Teacher::className(), ['teacher.id' => 'class.teacher_id']);
}
Proposal
When developing your models and query-classes always fully qualify the fields. You will never ever run into this problem again...that was my experience at least! I actually created my own model-gii-template. So this gets solved automatically now ;)
Hope it helped!

MS Access 2010- How to update field on one table based on field in another table

I'm designing a database to track requests. Currently, I have a form that its' record source is based off a query "Unassigned Requests". This query is based off my table Requests, and returns all "unassigned Requests". In this form, I would like the status field to change to "Assigned", once a Tech field has been assigned to the request. I currently have the Default for the Tech Assigned field set to "Blank', and the status field set to 'Unassigned". Both of these fields are combo boxes. The status field has a control source from the request table and row source from the status table. The tech assigned field has a control source from the Tech table and the row source is based off a query.
I have tried multiple solutions that have not seemed to work. I have limited experience with Macros and VBA. I would appreciate any suggestions on solving this problem.
I am going to make some assumptions regarding your post and if any are incorrect please let me know.
You have a request form where a request is assigned to a Tech. On that form there is a dropdown for the status, and also for the Tech.
You want to make it so when the tech dropdown is filled out with a tech's name you want the dropdown to change to assigned.
If this is the case I would recommend using the Tech Assigned field's AfterUpdate event. The code would look something like this:
Private Sub cboTech_Assigned_AfterUpdate()
If Nz(Me.cboTech_Assigned.Value, "") <> "" Then
Me.cboStatus = "Assigned"
Else
Me.cboStatus = ""
End If
End Sub
Obviously you will need to adjust to your own naming scheme. I should also point out that I don't even know if that Nz function is needed, I have just gotten into the habit of putting it everywhere. If I misunderstood something about what you want to do please let me know!

Access 2002 - display text on form

I have asked two similar questions, neither of which have been written with the necessary precision I need, but both of which have been answered correctly. I apologise for this and I will attempt to ask the question again with suitable precision.
I have a form on which I am attempting to display information from several tables. The form is called contacts1. The main database table the form is based on is a customer table called contacts, and is keyed on contact_id. I use the form to display information relating to a contact. I want to also display the address of a contact (in a text field called concatAddress). The address information is in a database table called address, keyed on address_id, and has a foreign key relationship with the contacts table via contact_id.
I have an expression I am trying to use to display the address: =DLookUp(" [address]![address_line_1] & [address]![ address_line_2] & [address]![city]","[concatAddress]","[address]![contact_id]=" & [Forms]![contacts1]![contact_id]). This gives me an #Error on my form. I am unsure what is causing this error.
Why would my expression cause the error please?
Oh dear. I have the answer: I forgot to include the table the address data is coming from - I mistook the table name field for the field the data is going into. So, this works:
=DLookUp("address_line_1 &" "& address_line_2 &" "& city","[address]","[address]![contact_id]=" & Forms!contacts1!contact_id) (I added spaces between the address fields and the city).
I hope this helps someone - I've been playing with this for a while now.

Creating a view in SQL - duplicate fields error

Hey there I am trying to learn SQL by trying to complete online questions and I'm trying to create a view.
I have created a SELECT query that works and now trying to turn it into a view, though when following the online instructions and enter the script that should create a view I come up with the duplicate field error.
I've looked into it and it may be "doctor.fullname" as a duplicate field but this is from another table. Andway I have deleted and still tried it but the error still appears.
I know there is probably an easy solution and sorry to bother you with this but it will be must appreciated.
CREATE VIEW patient_registration_form
AS
SELECT
patient.patient_id,
patient.nok_no,
patient.f_name,
patient.s_name,
patient.sex,
patient.dob,
patient.marital_status,
patient.date_registered,
nok.tel_no,
nok.full_name,
nok.address,
nok.relationship,
doctor.doctor_id,
doctor.clinic_no,
doctor.full_name,
doctor.address
FROM doctor, patient, nok
WHERE doctor.doctor_id = patient.doctor_id
AND nok.nok_no = patient.nok_no;
You're returning several columns with the same name... full_name and address... even though they're from different tables. In this case, you have to give them different aliases:
SELECT
patient.patient_id,
patient.nok_no,
patient.f_name,
patient.s_name,
patient.sex,
patient.dob,
patient.marital_status,
patient.date_registered,
nok.tel_no,
nok.full_name as nok_full_name,
nok.address as nok_address,
nok.relationship,
doctor.doctor_id,
doctor.clinic_no,
doctor.full_name as doctor_full_name,
doctor.address as doctor_address
You have two full_name listed... one for nok.full_name and one for doctor.full_name. One needs to be renamed such as nok.full_name as nok_full_name.